python - 如何检查 Selenium (Python) 中是否存在元素?
问题描述
您好我正在尝试从 HTML 中选择名称“Saleem”。它在一张桌子里面,但我不知道这是否相关。名称可能并不总是存在,因此如果名称包含在表中,我正在尝试找到一种选择元素的方法。例如,“Liam”在搜索时不会出现,但“saleem”会出现。如何单击搜索 saleem 时出现的链接?出于某种原因,Selenium 无法使用我在下面编写的代码找到元素。
这是网站(我只是将 Saleem 放在名称类别中并搜索): https ://sanctionssearch.ofac.treas.gov/default.aspx
我尝试了下面的代码,但不幸的是不起作用。
driver.find_element_by_id("btnDetails").click()
<a> id="btnDetails" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$gvSearchResults$ctl02$btnDetails", "", false, "", "Details.aspx?id=5839", false, true))" style="color:Blue">AL-IFRI, Saleem </a>
任何帮助表示赞赏!
解决方案
是的,你可以在里面使用它try..except
from selenium.common.exceptions import NoSuchElementException
# YOUR CODE
try:
webdriver.find_element_by_id('btnDetails')
except NoSuchElementException:
# Element does not exist
else:
# Element exists
推荐阅读
- r - 识别变量内所有项目中存在的通用文本
- html - 没有滚动条的背景图像
- python - 如何从同一文件夹和外部通用导入 Python 文件?
- javascript - Axios调用Springboot后端响应返回响应后不转发到新页面
- docker - 错误:在 Gitlab Runner 中找不到 pg_config 可执行文件
- python - 尝试使用 python3 webscraper
- java - 如何在 Quarkus 应用程序中检索 SecurityContext?
- javascript - 将鼠标悬停在一个 div 上以触发另一个 div 中的动画
- r - R GAM 交互代码突然停止工作
- java - 将 POJO 转换为 xml 时去掉内部类的类名