首页 > 解决方案 > Selenium 代码在本地运行但不在服务器上

问题描述

服务器和我的计算机都有 geckodriver 0.26.0、Firefox 71 和 Selenium 3.141.0。

我的电脑有带有 python 3.8 的 MacOS Mojave,服务器是带有 python 3.7 的 CentOS 7。该代码在我的计算机上完美运行,但它在服务器上返回错误。

我不记得是怎么回事了,但是我得到了不同的错误,这取决于我是否向它添加断点,或者我是否在终端中运行它或在 SLURM 中提交作业。

在终端:

  File "Main.py", line 230, in <module>
    main()
  File "Main.py", line 179, in main
    dfs=get_data(stations, inidate, findate)
  File "Main.py", line 113, in get_data
    list_files=return_list_day(date)
  File "Main.py", line 66, in return_list_day
    driver.get(webdir)
  File "/home/user/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/home/user/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/user/.local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Failed to decode response from marionette

或者

在 SLURM 上提交(最常见的错误):

Traceback (most recent call last):
  File "Main.py", line 230, in <module>
    main()
  File "Main.py", line 179, in main
    dfs=get_data(stations, inidate, findate)
  File "Main.py", line 113, in get_data
    list_files=return_list_day(date)
  File "Main.py", line 66, in return_list_day
    driver.get(webdir)
  File "/home/user/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/home/user/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/user/.local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Timeout loading page after 300000ms

我可以使用 Selenium 而不会在另一个完成得很好的不相关代码中出现问题。所以我不明白为什么 Selenium 在这里或它来自哪里会遇到困难。

代码在这里失败:

        ## Commented parts are cuz I tried running with the MOZ_HEADLESS tag on terminal 
        ## Didnt make a difference as far as I could tell.
        #options = Options()
        #options.headless = True
        driver = webdriver.Firefox()#options=options) 
        driver.get(webdir) ##<-- HERE
        ## Added these because of other replies to this issue I found
        driver.implicitly_wait(7)
        time.sleep(3)
        .
        .
        .

        driver.close()

那段代码从网页中检索列表并在for-loop 中运行。错误不在检索部分,在driver.get(webdir). 我无法共享该网站,因为它实际上就像查看合作机构的服务器一样。webdir是一个目录,我基本上等到它的内容被加载,所以我可以检索它的文件名。

我知道如果没有网站,您将无能为力,但是我所显示的错误是否可以说明问题可能是什么?检索特定网站的行为是否会因操作系统而异?我用谷歌搜索了这些错误,在这里找到了问题,通读并应用它们以查看它是如何变化的,但什么也没做,或者我得到了一个不同的错误(以上两个之一)。

我发现表明壁虎和 Mozilla 之间不兼容,但由于我可以成功运行另一个(不相关的)代码,准确调用和使用 Selenium(仅给出不同的 URL),所以我认为这不是我的问题。

谢谢你的帮助!让我知道我可以提供哪些其他信息可能会有所帮助。

编辑:

它与所链接的问题不同,因为我已经给它睡眠时间并且没有改变任何东西。它分配了 40 GB 的内存,因此它不会因为内存太少而死掉。这个问题中显示了哪些解决方案

标签: python-3.xseleniumselenium-webdriver

解决方案


对于遇到此问题的任何人,服务器将 Firefox 更新到版本 79.0,现在它可以正常工作了。据我所知,没有其他任何改变。

我假设版本更改修复了它,但我不知道具体如何。如果其他人遇到与我相同的错误,则值得一试,具体取决于它的运行方式。


推荐阅读