python - 我该如何做到,如果 XPATH 在网站上没有看到元素,它会跳过该行并移动到下一行?
问题描述
我该如何做到这一点,如果 XPATH 没有在页面上看到该元素,它会跳过该元素并移动到下一行?我的代码是:
1. driver.find_element_by_xpath('//img[@alt = "Black"]').click()
然后如果它没有找到第一个元素,它会跳过并尝试查找:
2. driver.find_element_by_xpath('//*[@id="add-remove-buttons"]/input').click()
我猜这与“如果显示___,单击,否则如果___单击”有关,尽管我不确定如何格式化。
解决方案
使用 try 和 catch 块并捕获 ElementNotFoundException 异常 Java:
try{
//your code to find element
}
catch(ElementNotFoundException)
{
//print element not found
}
// find another element and perform action
参考-https://www.w3schools.com/python/python_try_except.asp
Python:
try:
//your code
except ElementNotFoundException:
print("ELEMENT NOT fOUND")
推荐阅读
- c# - Ajax.BeginForm 完成时在 cshtml 中调用 ac# 函数
- vue.js - 在哪里放置应用程序中通用的 VueJS 组件
- c++11 - 我可以使用 using 关键字进行部分模板专业化吗?
- python - 添加一个新列,其值基于 groupby 值另外两列
- spring - Serenity + Cucumber + Spring 输出字节码
- laravel-5 - 为什么 $request 不在 laravel 中添加或删除
- javascript - 如何在我的 AngularJS 项目中包含 Chart.js?
- r - 在 Windows 上从私有 gitlab 服务器安装包
- sql-server - 使用 BCP 将 CSV 文件导入 Azure SQL Server
- ios - 其他页面上的变量转换错误