Selenium 不会在远程服务器上无头启动
Selenium won't Start headless on remote server
我尝试在远程服务器上使用 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
我用的是Python2.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 addons.webextension.doh-rollout@mozilla.org WARN Loading extension 'doh-rollout@mozilla.org': Reading manifest: Invalid extension permission: networkStatus
1588545829585 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: mozillaAddons
1588545829585 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: telemetry
1588545829585 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: resource://pdf.js/
1588545829585 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': 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":""}}
我应该指定其他内容吗?
解决了这样的问题:
- 重启节点以删除所有设置并锁定进程
- 使用
xvfbwrapper
而不是 运行 无头 Firefox
- 一定要关闭驱动和虚拟显示器
代码示例
from xvfbwrapper import Xvfb
from selenium import webdriver
vdisplay = Xvfb()
vdisplay.start()
driver = webdriver.Firefox()
driver.quit()
vdisplay.stop()
我尝试在远程服务器上使用 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
我用的是Python2.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 addons.webextension.doh-rollout@mozilla.org WARN Loading extension 'doh-rollout@mozilla.org': Reading manifest: Invalid extension permission: networkStatus
1588545829585 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: mozillaAddons
1588545829585 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: telemetry
1588545829585 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: resource://pdf.js/
1588545829585 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': 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":""}}
我应该指定其他内容吗?
解决了这样的问题:
- 重启节点以删除所有设置并锁定进程
- 使用
xvfbwrapper
而不是 运行 无头 Firefox - 一定要关闭驱动和虚拟显示器
代码示例
from xvfbwrapper import Xvfb
from selenium import webdriver
vdisplay = Xvfb()
vdisplay.start()
driver = webdriver.Firefox()
driver.quit()
vdisplay.stop()