Selenium代码在本地运行,但不在服务器上运行

时间:2020-08-05 00:04:10

标签: python-3.x selenium selenium-webdriver

服务器和计算机均具有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,而在另一个完全没有问题的无关代码中没有问题。所以我不明白为什么硒在这里或来自哪里很难。

代码在这里失败:

        ## 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循环内运行。该错误不在获取部分中,而在driver.get(webdir)中。我无法共享该网站,因为从字面上看,它就像调查合作伙伴机构的服务器一样。 webdir是一个目录,我基本上要等到其内容加载完毕后才能检索其文件名。

我知道,如果没有网站,您将无济于事,但是我显示的错误是否表明可能是什么问题?特定网站的检索能否基于操作系统而有所不同?我已经搜索了错误,在这里找到了问题,仔细阅读并应用了它们以查看其变化,但没有采取任何措施,或者我遇到了另一个错误(上述两个错误之一)。

我发现this指出了壁虎和Mozilla之间的不兼容性,但是由于我可以成功地运行另一个(不相关的)代码,并且调用和使用了Selenium(仅给出了不同的URL),因此我认为这不是我的问题。

感谢您的帮助!让我知道我能提供的其他信息可能会有所帮助。

编辑:

它与链接的问题不同,因为我给了它睡眠时间,并且没有进行任何更改。它分配了40 GB的内存,因此不会因内存不足而耗尽。 this question.

中显示了哪些解决方案

1 个答案:

答案 0 :(得分:0)

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

我认为版本更改已解决,但我不知道具体如何。如果其他人遇到的错误与我相同,这取决于运行方式,则值得尝试。