Xvfb:运行测试脚本时连接被拒绝

时间:2017-05-20 17:13:26

标签: 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/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()

我现在非常困难,非常感谢别人的帮助!

详细说明:

  • Firefox 53.0
  • Geckodriver 0.16.1
  • Fedora 22 x86_64

谢谢! - ck

1 个答案:

答案 0 :(得分:0)

您正在使用两种不同的方法来使用Xvfb:从命令行运行它,并从pyvirtualdisplay运行它。命令行方法不起作用的原因是因为您没有将新的Xvfb实例连接到系统显示器,并且pyvirtualdisplay方法无法工作的原因是因为您尝试在pyvirtualdisplay创建之前实例化浏览器浏览器实例运行的虚拟帧缓冲区。选择一种方法,但不要同时执行这两种操作。

如果要从命令行运行它,还必须导出DISPLAY以匹配您设置的端口:

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()