python - 当在硒中找不到项目时如何引发错误并继续使用不同的功能
问题描述
我正在尝试在网页中搜索链接文本,并且如果找不到该项目并移动到其他内容,我正在尝试使程序响应而不会出现错误。
from selenium.common.exceptions import NoSuchElementException
hasElementQ = True
while True:
try:
elem = browser.find_element_by_link_text('pant')
break
except NoSuchElementException:
print ('it doesnt exist')
这是我收到的错误:
except NoSuchElementException:
^
SyntaxError: invalid syntax
解决方案
您except
需要与您的缩进级别相同try
from selenium.common.exceptions import NoSuchElementException
hasElementQ = True
while True:
try:
elem = browser.find_element_by_link_text('pant')
break
except NoSuchElementException:
print ('it doesnt exist')
推荐阅读
- css - 应用 TranslateY 时,Firefox 以不同方式呈现元素
- java - 为什么选择数组列表
可以分配给 ArrayList - python - 使用 keras 进行迁移学习
- php - 有没有办法根据用户帐户唯一地查看数据?代码点火器 PHP
- eclipse - eclipse:奇怪的背景颜色突出显示
- linux - “无法执行二进制文件:执行格式错误”在 aarch64 处理器 Debian 10 上运行的 x84-64 位程序
- visual-studio - 0 总测试 - 0 通过,0 失败,0 即使在课堂上进行测试后也跳过
- arrays - 在 PostgreSQL 中,如何从列表中提取匹配项?
- jdbc - SoapUI JDBC 请求步骤 - [错误 3706] [SQLState 42000] 语法错误:数据类型与定义的类型名称不匹配
- java - 使用带有@JsonTypeName 的EXTERNAL_PROPERTY 时,序列化字符串中的两个类型字段