Selenium Python—如果元素不存在,则执行此操作,否则
我绝对不是硒专家,因此我谦卑地来到这里寻求帮助。在执行了下面的python脚本之后,我认为如果在两次打印之间失败,我可以简单地查看日志并查看最后一次打印的位置。不幸的是,脚本一直运行到最后,并打印所有内容,无论是否成功。以下是我所拥有的:Selenium Python—如果元素不存在,则执行此操作,否则,python,selenium-webdriver,Python,Selenium Webdriver,我绝对不是硒专家,因此我谦卑地来到这里寻求帮助。在执行了下面的python脚本之后,我认为如果在两次打印之间失败,我可以简单地查看日志并查看最后一次打印的位置。不幸的是,脚本一直运行到最后,并打印所有内容,无论是否成功。以下是我所拥有的: print("Attempting, map zoom 4x.") driver.find_element_by_xpath("//*[@class=\"leaflet-control-zoom-in\"]").click() driver.find_elem
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设置格式?感谢您的帮助。谢谢。您可以尝试以下方法:
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"
谢谢卡杜德里诺,我现在就来试试。将报告结果。似乎selenium挂在驱动程序上。查找…等。因为它找不到要开始的元素。我试着直接转到if语句。if driver.find_element_by_xpath(…):也会失败。print(“试图在任务列表中突出显示‘任务名称’”)if self.find_element_(by.xpath,“/*[contains(text(),'123')]”:print(“element find.Clicking highlight.”)driver.find_element_by_xpath(“/*[contains(text(),'123')”)“,'123')]”。单击()其他:self.fail(“单击突出显示失败。找不到元素?”)很抱歉格式化。无法理解
code
hmm我从未尝试过的元素是否存在。我得检查一下