【问题标题】:selenium.common.exceptions.TimeoutException error using WebDriverWait with expected_conditions through Selenium and Pythonselenium.common.exceptions.TimeoutException 错误通过 Selenium 和 Python 使用 WebDriverWait 和 expected_conditions
【发布时间】:2020-11-26 23:55:42
【问题描述】:
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 服务器实例的上述异常。我的脚本在本地系统上使用任何版本的 firefox 和 geckodriver 的 Ubuntu 和 Mac 操作系统都可以正常工作。但是 EC2 ubuntu 18.04.01 版本出现上述错误,在此我也尝试升级和降级 firefox 和 geckodriver 版本,但仍然无法正常工作。谁能帮我提供建议和解决方案。

【问题讨论】:

  • 您确定应用程序已启动并导航到您拥有 ID 为 FavoriteApp_ITEM 的元素的页面吗?
  • 是的,它可以在本地 ubuntu 和 mac os 上正常工作,但在 EC2 出现问题时,我也尝试升级和降级 firefox 和 gecko 驱动程序不适合我
  • 意思是,在EC2中运行脚本时firefox实例没有启动?
  • 最初,登录正在工作 FavoriteApp_ITEM not working here 在调试模式下运行 python 脚本时,EC2 服务器上出现了一个奇怪的问题,它工作得非常好
  • 脚本工作的初始阶段,但它卡在FavoriteApp_ITEM 在服务器上出现上述问题

标签: python selenium webdriverwait timeoutexception expected-condition


【解决方案1】:

此错误消息...

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


WebDriverWait

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

因此,无论使用expected_conditionsWebDriverWait 中的哪一个,失败都会导致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) 减慢搜索诱导等待时间。


参考文献

您可以在以下位置找到一些相关讨论:

【讨论】:

  • 谢谢@DebanjanB,你是对的,我已经按照你的建议更改了脚本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
  • 2020-01-14
  • 1970-01-01
  • 2020-10-26
  • 2019-08-26
相关资源
最近更新 更多