![Xvfb:在 Fedora 上通过浏览器运行测试脚本时连接被拒绝](https://linux22.com/image/1530607/Xvfb%EF%BC%9A%E5%9C%A8%20Fedora%20%E4%B8%8A%E9%80%9A%E8%BF%87%E6%B5%8F%E8%A7%88%E5%99%A8%E8%BF%90%E8%A1%8C%E6%B5%8B%E8%AF%95%E8%84%9A%E6%9C%AC%E6%97%B6%E8%BF%9E%E6%8E%A5%E8%A2%AB%E6%8B%92%E7%BB%9D.png)
我有一个 python 脚本,只有通过调用才会触发my.ip.address/test
。如果我通过命令行运行 PHP 代码,它就可以正常工作。
但是,如果我使用指定的 URL 通过浏览器访问测试自动化,就会出现此错误:
Traceback (most recent call last): File "scripts/crawler.py",
line 10, in driver = webdriver.Firefox(capabilities={"marionette":True}) File "/usr/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py",
line 152, in __init__ keep_alive=True) File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 98, in __init__ self.start_session(desired_capabilities, browser_profile) File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 188, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 252, in execute self.error_handler.check_response(response) File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: connection refused
geckodriver.log 中的错误:
1495299180874 geckodriver::marionette INFO Starting browser /usr/lib/firefox/firefox with args ["-marionette"]
Unable to init server: Could not connect: Connection refused
Error: cannot open display: :99
简单的 Python 代码:
#!/usr/bin/env python2
from pyvirtualdisplay import Display
from selenium import webdriver
import time
import sys
driver = webdriver.Firefox(capabilities={"marionette":True})
display = Display(visible=0, size=(800,600))
display.start()
driver.get('https://www.google.com')
print driver.title
driver.close()
display.stop()
端点的 PHP 代码片段/test
:
$cmd = 'xvfb-run -a python scripts/crawler.py'
return shell_exec($cmd);
已经安装Xvfb
并运行:
$ whoami
codekaizer #with root privileges
$ Xvfb :99 -screen 0 1024x768x24 -ac -fbdir /tmp/.X11-unix/X99 &
我已经验证/tmp/.X11-unix/X99
套接字正在监听该Xvfb
进程。
我现在很困惑,非常感谢大家的帮助!
细节:
- Firefox 53.0
- Geckodriver 0.16.1
- Fedora 22 x86_64
谢谢!- ck