python-3.x - 如何使用“if”用python测试网页中元素的存在?
问题描述
我创建了一个循环(while True)
来使用 python 自动执行站点上的任务。此代码单击两个字段,直到页面上出现一个元素
(browser.find_element_by_id ('formComp: buttonBack')
.
当这个元素出现时,我希望循环停止并转到下一个代码块。
我是这样测试的,但它犯了一个错误。"formComp: buttonback"
Python 报告找不到该元素。但这就是它,如果没有找到继续循环:
while (browser.find_element_by_id('formComp:repeatCompromissoLista:0:tableRealizacao:0:subtableVinculacoes:0:vinculacao_input')):
vinc = wait.until(EC.presence_of_element_located((By.ID, 'formComp:repeatCompromissoLista:0:tableRealizacao:0:subtableVinculacoes:0:vinculacao_input')))
vinc = browser.find_element_by_id('formComp:repeatCompromissoLista:0:tableRealizacao:0:subtableVinculacoes:0:vinculacao_input')
vinc.send_keys('400')
enterElem5 = wait.until(EC.element_to_be_clickable((By.ID, 'formComp:buttonConfirmar')))
enterElem5 = browser.find_element_by_id('formComp:buttonConfirmar')
enterElem5.send_keys(Keys.ENTER)
time.sleep(int(segundosv))
if (browser.find_element_by_id('formComp:buttonRetornar')== True):
break
else:
continue
解决方案
尝试这样希望这会有所帮助length
。检查按钮的计数是否超过 0。
if (len(browser.find_elements_by_id('formComp:buttonRetornar'))>0):
break
else:
continue
推荐阅读
- django - 如何使用 NGINX 为 Django 应用程序的用户 www-data 设置 AWS 凭证
- node.js - 使用 Gmail API 发送电子邮件
- c++ - 难以从 c++ boost 库中获取 barycentric_rational 以按照我的需要工作
- c# - Visual Studio 代码 - 参考文件夹
- macos - 在 PlaidML 中使用实验设备支持?
- java - 如何防止删除液
- python-3.x - Groupby 与 Cumcount - 未按预期工作
- python - 基于数据库引擎的 Alembic 条件
- python - 等价于Python对R中字符串的综合列表操作
- environment-variables - 获取 Dlang 中的所有环境变量