gpt4 book ai didi

python - Selenium Python - 处理没有这样的元素异常

转载 作者:太空狗 更新时间:2023-10-29 20:32:27 24 4
gpt4 key购买 nike

我正在使用 Python 在 Selenium 中编写自动化测试。一种元素可能存在也可能不存在。我正在尝试使用以下代码处理它,它在元素存在时起作用。但是当元素不存在时脚本失败,如果元素不存在我想继续下一个语句。

try:
elem = driver.find_element_by_xpath(".//*[@id='SORM_TB_ACTION0']")
elem.click()
except nosuchelementexception:
pass

错误-

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:{"method":"xpath","selector":".//*[@id='SORM_TB_ACTION0']"}

最佳答案

你不导入异常吗?

from selenium.common.exceptions import NoSuchElementException

try:
elem = driver.find_element_by_xpath(".//*[@id='SORM_TB_ACTION0']")
elem.click()
except NoSuchElementException: #spelling error making this code not work as expected
pass

关于python - Selenium Python - 处理没有这样的元素异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38022658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com