我有一个 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