python - 使用带有 Python 的 Selenium Webdriver 在 Headless chrome 浏览器上运行脚本时发生超时错误

标签 python google-chrome selenium selenium-webdriver headless-browser

当我在 Headless Chrome Broswer (Webdriver + Selenium) 上运行 python 脚本测试网站时,我们经常遇到超时错误,我发现问题发生在脚本与浏览器交互时 .click().send_keys() 方法。任何人都可以知道它是什么样的问题吗?有时它工作正常但有时我遇到超时错误。请给出相同的解决方案

堆栈跟踪:

 15:01:48,194 root:ERROR: ERROR occurred: Message: timeout
   (Session info: headless chrome=60.0.3112.101)
   (Driver info: chromedriver=2.31.488763 
   (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 
    x86)

    Traceback (most recent call last):
  File "c:\autotest\x.py", line 148, in main
    func(nik)
  File "c:\autotest\lib\support.py", line 126, in wrapper
    raise ret
  File "c:\autotest\lib\support.py", line 113, in newFunc
    res[0] = func(*args, **kwargs)
  File "c:\autotest\testcases\1001.py", line 15, in testcase
    "documents_approved ASC", generateError=True)  
  File "c:\autotest\lib\support.py", line 51, in wrapper
    f_result = func(*args, **kwds)
  File "c:\autotest\pageobjects\web\segment_header.py", line 184, in login
    + Keys.ENTER)
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 350, in send_keys
    'value': keys_to_typing(value)})
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 499, in _execute
    return self._parent.execute(command, params)
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout
  (Session info: headless chrome=60.0.3112.101)
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 
    x86) 

最佳答案

我遇到了类似的问题,正常的 Chrome 驱动程序工作正常,但 headless chrome 总是超时。

我发现对于响应式网页,您需要设置窗口大小:

driver.set_window_size(1200, 600)

在驱动程序本身初始化后添加此行后它起作用了。

希望对您有所帮助!

关于python - 使用带有 Python 的 Selenium Webdriver 在 Headless chrome 浏览器上运行脚本时发生超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45798842/

相关文章:

python - 如何测试 pytest fixture 是否引发异常?

python - Python中如何从 'if'语句继续到 'elif'?

javascript - 如果您在 Chrome 的控制台中尝试 9n**9n**9n,Chrome 会中断(类似于无限循环)。为什么会这样?

java - 使用 Java 将 Firefox 的 Selenium IDE 转换为 Chrome 和 Internet Explorer

unit-testing - 我们如何从 TestNG 的失败消息中删除 java.lang.AssertionError

python - 如何在 SqlAlchemy 中查询具有分钟/小时步长间隔的行?

python - 如何在所有已安装的软件包离线的情况下重新创建 python 虚拟环境?

google-chrome - 为多个页面/URL 运行 lighthouse

javascript - 应该首先加载脚本还是图像?

java - Selenium - 查找网页的所有元素