Python 如果元素不存在,则处理Selenium时出错';不存在

Python 如果元素不存在,则处理Selenium时出错';不存在,python,selenium,Python,Selenium,如果我的find\u element\u by\u xpath返回有效元素或不是 price = driver.find_element_by_xpath("//div[@class='price']").text location = driver.find_element_by_xpath("//div[@class='listing__address']").text if (SOMETHINGGOESHEREIFELEMENTEXISTS) print(price, locat

如果我的find\u element\u by\u xpath返回有效元素或不是

price = driver.find_element_by_xpath("//div[@class='price']").text
location = driver.find_element_by_xpath("//div[@class='listing__address']").text

if (SOMETHINGGOESHEREIFELEMENTEXISTS)
    print(price, location)
    driver.quit()
else:
    print("QUITING!")
    driver.quit()
在这两种情况下,我都想做一个驱动程序。退出

谢谢。

根据,
find\u element\u by\u xpath()
在失败时引发
NoSuchElementException
异常。那么,以下几点应该起作用:

试试看:
price=driver。通过xpath(“//div[@class='price'])查找元素。text
location=driver.find_element_by_xpath(“//div[@class='listing_地址'])。text
打印(价格、位置)
除无任何例外:
打印(“退出!”)
driver.quit()
根据,
find\u element\u by\u xpath()
在失败时引发一个
NoTouchElementException
异常。那么,以下几点应该起作用:

试试看:
price=driver。通过xpath(“//div[@class='price'])查找元素。text
location=driver.find_element_by_xpath(“//div[@class='listing_地址'])。text
打印(价格、位置)
除无任何例外:
打印(“退出!”)
driver.quit()