为了账号安全,请及时绑定邮箱和手机立即绑定

通过 Selenium 和 Python 使用带有 expected_conditions

通过 Selenium 和 Python 使用带有 expected_conditions

慕姐8265434 2023-03-22 16:43:35
Traceback (most recent call last):  File "Inventorytest.py", line 88, in <module>    j.go_to_application()  File "Inventorytest.py", line 65, in go_to_application    EC.element_to_be_clickable((By.ID, 'FavoriteApp_ITEM'))  File "/home/naroladev/Mercury_Back-End/mercuryenv/lib/python3.6/site-packages/selenium/webdriver/support/wait.py", line 80, in until    raise TimeoutException(message, screen, stacktrace)selenium.common.exceptions.TimeoutException: Message: 我在 EC2 服务器实例上遇到了异常。我的脚本在 Ubuntu 和 Mac OS 以及本地系统上任何版本的 firefox 和 geckodriver 上都可以正常工作。但是在 EC2 ubuntu 18.04.01 版本上遇到了上述错误,在此我也尝试升级和降级 firefox 和 geckodriver 版本但仍然无法正常工作。谁能帮我提出建议和解决方案。
查看完整描述

1 回答

?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

这个错误信息...


Traceback (most recent call last):

  File "Inventorytest.py", line 88, in <module>

    j.go_to_application()

  File "Inventorytest.py", line 65, in go_to_application

    EC.element_to_be_clickable((By.ID, 'FavoriteApp_ITEM'))

  File "/home/naroladev/Mercury_Back-End/mercuryenv/lib/python3.6/site-packages/selenium/webdriver/support/wait.py", line 80, in until

    raise TimeoutException(message, screen, stacktrace)

selenium.common.exceptions.TimeoutException: Message: 

...意味着WebDriver变体无法在构建WebDriverWait的时间范围内找到所需的WebElement 。


WebDriver等待

WebDriverWait构造函数将WebDriver实例作为参数和超时(以秒为单位)。

因此,无论使用哪一个expected_conditions,WebDriverWait失败都会导致TimeoutException。


这个用例

在这个用例中,行:

EC.element_to_be_clickable((By.ID, 'FavoriteApp_ITEM'))

无法在所需的时间范围内识别所需的元素,因此您遇到了TimeoutException

然而,从TimeoutException中很难挖掘出失败的实际结果。


解决方案

作为了解失败确切原因的解决方案,您需要删除 WebDriverWait并将代码行替换为:

  • find_element_by_class_name(name)

  • find_element_by_css_selector(css_selector)

  • find_element_by_id(id)

  • find_element_by_link_text(link_text)

  • find_element_by_name(name)

  • find_element_by_partial_link_text(partial_link_text)

  • find_element_by_tag_name(tag_name)

  • find_element_by_xpath(xpath)

如果需要,您可以在调试时减慢搜索诱导等待的速度time.sleep(secs)


查看完整回答
反对 回复 2023-03-22
  • 1 回答
  • 0 关注
  • 82 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信