Php Xvfb:运行测试脚本时连接被拒绝
我有一个python脚本,它只能通过调用Php Xvfb:运行测试脚本时连接被拒绝,php,python,selenium,selenium-webdriver,xvfb,Php,Python,Selenium,Selenium Webdriver,Xvfb,我有一个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
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
已安装Xvfb
并运行它:
$ whoami
codekaizer #with root privileges
$ Xvfb :99 -screen 0 1024x768x24 -ac -fbdir /tmp/.X11-unix/X99 &
为/test
端点运行PHP代码段:
$cmd = 'xvfb-run -a python scripts/crawler.py'
return shell_exec($cmd);
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()
我现在陷入困境,非常感谢有人的帮助
详情:
- 火狐53.0
- 壁虎河0.16.1
- Fedora 22 x86_64
-ck您混合了两种不同的方法来使用Xvfb:从命令行运行它,从pyvirtualdisplay运行它。命令行方法不起作用的原因是您没有将新的Xvfb实例连接到系统显示器,而pyvirtualdisplay方法不起作用的原因是您试图在pyvirtualdisplay为您的浏览器实例创建虚拟帧缓冲区之前实例化浏览器。选择一种方法,但不要两者都做 如果要从命令行运行,还必须导出显示器以匹配设置的端口:
Xvfb :99 -screen 0 1024x768x24 -ac -fbdir /tmp/.X11-unix/X99 &
export DISPLAY=:99
python yourscript.py
或者,更好的方法是让pyvirtualdisplay以编程方式管理所有这些,就像您几乎正在做的那样:
#!/usr/bin/env python2
from pyvirtualdisplay import Display
from selenium import webdriver
import time
import sys
# Use an context manager to handle the start() and stop()
# Instantiate Display BEFORE you try to instantiate the driver
with Display(visible=0, size=(800,600)):
driver = webdriver.Firefox(capabilities={"marionette":True})
try:
driver.get('https://www.google.com')
print driver.title
finally:
# A try/finally block like this insures the driver is closed even if an exception is thrown
driver.close()
您混合了两种不同的方法来使用Xvfb:从命令行运行它,从pyvirtualdisplay运行它。命令行方法不起作用的原因是您没有将新的Xvfb实例连接到系统显示器,而pyvirtualdisplay方法不起作用的原因是您试图在pyvirtualdisplay为您的浏览器实例创建虚拟帧缓冲区之前实例化浏览器。选择一种方法,但不要两者都做 如果要从命令行运行,还必须导出显示器以匹配设置的端口:
Xvfb :99 -screen 0 1024x768x24 -ac -fbdir /tmp/.X11-unix/X99 &
export DISPLAY=:99
python yourscript.py
或者,更好的方法是让pyvirtualdisplay以编程方式管理所有这些,就像您几乎正在做的那样:
#!/usr/bin/env python2
from pyvirtualdisplay import Display
from selenium import webdriver
import time
import sys
# Use an context manager to handle the start() and stop()
# Instantiate Display BEFORE you try to instantiate the driver
with Display(visible=0, size=(800,600)):
driver = webdriver.Firefox(capabilities={"marionette":True})
try:
driver.get('https://www.google.com')
print driver.title
finally:
# A try/finally block like this insures the driver is closed even if an exception is thrown
driver.close()
在实例化浏览器之前,应该先实例化显示。把那些线路换一下,告诉我它是否有效。嗨@LeviNoecker,谢谢。我已经更新了我的帖子。我先运行了Xvfb,然后运行了浏览器。它还不能工作。期待着您的想法。您应该在实例化浏览器之前实例化显示。把那些线路换一下,告诉我它是否有效。嗨@LeviNoecker,谢谢。我已经更新了我的帖子。我先运行了Xvfb,然后运行了浏览器。它还不能工作。期待着您的想法。