python - Python Selenium - 无效的语法错误,尝试除外
问题描述
我一直在尝试编写一个 Python 脚本,该脚本导入并运行 Selenium 以模拟浏览器浏览网页,并且随着相关页面动态更新(ajax),某些元素将变得可用并被销毁,以便处理案例在哪里click()
或send_keys()
不再存在的元素,我正在尝试实现try: except:
来处理这个 - 代码示例如下:
SuspendedBanner = driver.find_elements_by_class_name('suspended-label ng-scope')
CheckInPlay = driver.find_elements_by_class_name('market-status-label')
if len(SuspendedBanner) == 0 and CheckInPlay(0).text == 'In-Play':
try:
driver.find_elements_by_class_name('rh-back-all-label ng-binding')(0).click()
PriceInputs = driver.find_elements_by_class_name('find_elements_by_class_name')
if len(PriceInputs) > 4:
for PriceInput in PriceInputs:
PriceInput.send_keys('1.01')
BackButtons = driver.find_elements_by_class_name('back mv-bet-button back-button back-selection-button')
if len(BackButtons) == Len(PriceInputs):
for Button in BackButtons:
Prices.append(Button.find_elements_by_class_name('bet-button-price')[0].text
#print(Prices)
except:
pass
解决方案
你少了一个括号。在此处显示的第一行中,有两个左括号,但只有一个右括号。
Prices.append(Button.find_elements_by_class_name('bet-button-price')[0].text
#print(Prices)
except:
pass
通常情况下,语法错误消息将指向缺少括号(或]
,等)的那一行之后的一行。
推荐阅读
- django - 为 django 模型指定数据库名称
- python - VSCode 中未解决的 PyDev 导入错误?
- reactjs - 为 React 列表项生成稳定、可预测和唯一键的方法是什么?
- entity-framework - Entity Framework Core 3.1 SaveChangesAsync() 自动将异常记录到 Windows 事件查看器
- react-native - 反应原生 apk 生成
- hibernate - Grails Gorm dynamicUpdate 不会改变更新查询
- javascript - 按下浏览器上的后退按钮后 Javascript 代码不起作用
- ios - 快速计算嵌套数组的元素
- svelte - 苗条路由器 - 如何根据路由更改标头?
- postgresql - Postgres 查找过程和用户定义函数中使用的类型