Python 在无法连接的xvfb上运行Selenium测试
我正在尝试运行一个脚本,该脚本使用SeleniumFirefoxWebDriver运行多个测试。 它在本地机器上工作完美,但在xvfb上运行时却失败惨重 该机器是CentOS 6.8版(最终版) Firefox版本45.6.0 我正在使用Python/木偶 该命令与此类似:Python 在无法连接的xvfb上运行Selenium测试,python,selenium,firefox,selenium-webdriver,xvfb,Python,Selenium,Firefox,Selenium Webdriver,Xvfb,我正在尝试运行一个脚本,该脚本使用SeleniumFirefoxWebDriver运行多个测试。 它在本地机器上工作完美,但在xvfb上运行时却失败惨重 该机器是CentOS 6.8版(最终版) Firefox版本45.6.0 我正在使用Python/木偶 该命令与此类似: xvfb运行--server args=“-screen 0,1920x1080x24”我的程序 以这种方式运行时,会出现一些与不加载页面相关的错误。 所以我得到了一些截图,我看到的只是“无法连接”的Firefox屏幕 起初
xvfb运行--server args=“-screen 0,1920x1080x24”我的程序
以这种方式运行时,会出现一些与不加载页面相关的错误。
所以我得到了一些截图,我看到的只是“无法连接”的Firefox屏幕
起初我认为它可能与代理有关。。。我已经暗示没有禁用代理,一个简单的“wget”会像预期的那样工作。
但后来我在代码中强制使用Firefox首选项,这样它就不用代理了,对吧
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 0)
同样的结果。
因此,我在谷歌上搜索了类似的情况,找到了一些要求在命令行中添加显示编号的答案。
因此,我将命令行更改为:
export DISPLAY=:1
xvfb-run --server-args=":1 -screen 0, 1920x1080x24" MyProgram
然后我得到了一个不同的错误,但仍然不起作用:
ERROR: WebDriverException: connection refused Traceback (most recent call last):
我还尝试记录更多信息,将-e参数添加到xvfb run,但得到的只是一个空文件
你知道我还能做些什么吗
*更新*
这里有一个小代码来重现这个问题
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import *
display = Display(visible=0, size=(1920, 1080))
display.start()
profile = webdriver.FirefoxProfile()
profile.set_preference("network.http.phishy-userpass-length", 255);
profile.set_preference("network.proxy.type", 0)
capabilities = None
# Marionette not necessary as it's Firefox 45
# capabilities = DesiredCapabilities.FIREFOX
# capabilities["marionette"] = True
print("Getting webdriver...")
browser = webdriver.Firefox(firefox_profile=profile, capabilities=capabilities)
print("Requesting URL...")
browser.get('https://www.google.com')
print("TITLE:", browser.title)
browser.quit()
display.stop()
输出:
Getting webdriver...
Requesting URL...
TITLE: Problem loading page
可能会有帮助。@barakmanos谢谢,但我已经尝试了这个链接的解决方案,但仍然不起作用。我用一个示例代码更新了这个问题。你在那篇文章中检查过我的答案了吗?好的,您需要确保Firefox在服务器上的特定位置,并在代码中使用该位置。您的意思是设置FirefoxBinary路径?是的,我也试过这个。