Selenium不会在远程服务器上无头启动

问题描述 投票:0回答:1

我尝试使用远程服务器上的Selenium自动执行Web上的例行任务。

因此,当我尝试在python中启动webdriver时,它失败并显示connection refused错误:

instance-1@instance-1:~$ python
Python 2.7.17 (default, Apr 15 2020, 17:20:14) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> from selenium.webdriver.firefox.options import Options
>>> options = Options()
>>> options.headless = True
>>> options.log.level = "trace"
>>> driver = webdriver.Firefox(options=options )


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/instance1/.local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
  File "/home/instance-1/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/instance-1/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/instance-1/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/instance-1/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: connection refused

我使用Python 2.7和Firefox,详细版本如下。

instance-1@instance-1:~$ 
instance-1@instance-1:~$ which firefox
/usr/bin/firefox
instance-1@instance-1:~$ firefox --version
Mozilla Firefox 75.0
instance-1@instance-1:~$ python -c "import selenium; print(selenium.__version__)"
3.141.0
instance-1@instance-1:~$ geckodriver --version 
geckodriver 0.26.0 (e9783a644016 2019-10-10 13:38 +0000)

The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.

This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.
instance-1@instance-1:~$ which geckodriver 
/usr/local/bin/geckodriver
instance-1@instance-1:~$ 

而且,我已经像这样通过Xvbf设置了虚拟显示

instance-1@instance-1:~$ Xvfb :99 -ac &
[1] 16171
instance-1@instance-1:~$ export DISPLAY=:99

我的geckotrace输出是

1588545824498   mozrunner::runner       INFO    Running command: "/usr/bin/firefox" "-marionette" "-headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofileOn8vbe"
1588545824519   geckodriver::marionette DEBUG   Waiting 60s to connect to browser on 127.0.0.1:44835
*** You are running in headless mode.
1588545828833   [email protected]     WARN    Loading extension '[email protected]': Reading manifest: Invalid extension permission: networkStatus
1588545829585   [email protected]     WARN    Loading extension '[email protected]': Reading manifest: Invalid extension permission: mozillaAddons
1588545829585   [email protected]     WARN    Loading extension '[email protected]': Reading manifest: Invalid extension permission: telemetry
1588545829585   [email protected]     WARN    Loading extension '[email protected]': Reading manifest: Invalid extension permission: resource://pdf.js/
1588545829585   [email protected]     WARN    Loading extension '[email protected]': Reading manifest: Invalid extension permission: about:reader*
1588545830466   Marionette      TRACE   Marionette enabled
1588545831067   Marionette      TRACE   Received observer notification toplevel-window-ready
1588545884800   mozrunner::runner       DEBUG   Killing process 16183
Exiting due to channel error.
Exiting due to channel error.
1588545885811   webdriver::server       DEBUG   <- 500 Internal Server Error {"value":{"error":"timeout","message":"connection refused","stacktrace":""}}

我还应该指定其他内容吗?

python selenium selenium-webdriver firefox mozilla
1个回答
0
投票

已解决的问题:

  • 重新启动节点以删除所有设置并锁定进程
  • 使用xvfbwrapper而不是运行无头Firefox
  • 请确保关闭驱动程序和虚拟显示

代码示例

from xvfbwrapper import Xvfb
from selenium import webdriver
vdisplay = Xvfb()
vdisplay.start()
driver = webdriver.Firefox()
driver.quit()
vdisplay.stop()
© www.soinside.com 2019 - 2024. All rights reserved.