3

我绝不是硒专家,因此我谦虚地来这里寻求帮助。在执行下面的 python 脚本后,我想如果它在两次打印之间失败,我可以简单地查看日志并查看最后一次打印的位置。不幸的是,脚本会一直运行到最后并打印所有内容,无论它是否成功。这是我所拥有的:

print("Attempting, map zoom 4x.")
driver.find_element_by_xpath("//*[@class=\"leaflet-control-zoom-in\"]").click()
driver.find_element_by_xpath("//*[@class=\"leaflet-control-zoom-in\"]").click()
driver.find_element_by_xpath("//*[@class=\"leaflet-control-zoom-in\"]").click()
driver.find_element_by_xpath("//*[@class=\"leaflet-control-zoom-in\"]").click()
print("Zoom in 4x. Successful")

我想要做的是以下几点:

print("Attempting, map zoom 4x.")
if driver.find_element_by_xpath ...... exists,
then
   driver.find_element_by_xpath("//*[@class=\"leaflet-control-zoom-in\"]").click()
   driver.find_element_by_xpath("//*[@class=\"leaflet-control-zoom-in\"]").click()
   driver.find_element_by_xpath("//*[@class=\"leaflet-control-zoom-in\"]").click()
   driver.find_element_by_xpath("//*[@class=\"leaflet-control-zoom-in\"]").click()
   print("Zoom in 4x. Successful")
else
   print("Element does not exist, it failed.")

我将如何为 python/selenium 格式化?任何帮助表示赞赏。谢谢你。

4

1 回答 1

3

您可以尝试以下方法:

print("Attempting, map zoom 4x.")
elem = driver.find_element_by_xpath('/xpath/to/your/element/here')
if elem.is_displayed():
    elem.click()
    print("Zoom in 4x. Successful")
else:
    print "Element is not visible"
于 2013-10-09T02:07:35.997 回答