如何在 Xvfb 上运行无头 Firefox

Posted

技术标签:

【中文标题】如何在 Xvfb 上运行无头 Firefox【英文标题】:How to run headless Firefox on Xvfb 【发布时间】:2018-02-15 00:07:22 【问题描述】:

我正在尝试使用 geckodriver 运行 webdriver。我已经安装了 selenium gecko、firefox 和 python27。我的文件夹有脚本和 geckodriver。

当我从 centOS 运行命令 python script.py 时,它会说

selenium.common.exceptions.WebDriverException: Message: Failed to start browser: permission denied

下面是我正在使用的脚本:

#!/usr/bin/env python
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('/usr/local/firefox')
display = Display(visible=0, size=(800, 600))
display.start()

browser = webdriver.Firefox(firefox_binary=binary)

browser.get('http://www.google.com')
print browser.title
browser.quit()

display.stop()

在你问之前,当我运行whereis firefox 时,我得到firefox: /usr/local/bin/firefox /usr/local/firefox

编辑:

在我运行sudo yum -y install firefox Xvfb libXfont Xorg 后,python scipt 启动,但我在 geckdriverlog 上收到错误消息:

XPCOMGlueLoad error for file /usr/local/firefox/libxul.so:
libdbus-glib-1.so.2: cannot open shared object file: No such file or directory
Couldn't load XPCOM.

我认为这是由于this 错误。因此我尝试了sudo yum install libdbus-glib-1-2,但它说,No package libdbus-glib-1-2 available.

编辑 2:

所以也许我应该问(这可能是问题的答案); 如何在 CentOS 上使用终端安装 libdbus-glib-1-2 从 here 获取库?

编辑 3:

所以基本上要运行无头 Firefox,我需要 Xvfb,但是当我运行它 Xvfb :19 -screen 0 1024x768x16 & 时,我会卡在 Initializing built-in extension GLX 上,就像 this 问题一样。所以我认为问题归结为。

最后我要补充一点,Firefox 版本的东西就是一团糟。

【问题讨论】:

你在 mac 上吗? 没有在 CentOS 上 只有在无头运行时才会出现错误?或者错误也很严重。 我不能乱跑,因为它只是一个 aws 服务器。 我真的不知道如何处理赏金,因为它在 13 小时结束,我仍然没有得到正确的答案。 【参考方案1】:

发现其他人也有同样的问题,虽然你试过which,你能检查/usr/local/bin/firefox/firefox是否可行?

【讨论】:

我试过了(由于我更新了一些东西,再次检查问题,它说现在编辑中的错误) 浏览器 = webdriver.Firefox() 而不是任何配置文件怎么样。 这不再是问题了,因为当我在终端上写 firefox 时,我得到了 libdbus-glib-1.so.2: cannot open shared object file: No such file or directory Couldn't load XPCOM. 所以我认为这就是他们现在的问题。

以上是关于如何在 Xvfb 上运行无头 Firefox的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Robot Framework 中运行无头测试

Windows 上的 Xvfb

在 Selenium Grid 上运行 Firefox 测试时在 xvfb 显示屏幕之间切换

有没有办法在远程主机上运行 Selenium 测试?

如何使用无头镀铬在 Selenium 中启用闪存

如何在 google appengine 上为 node.js 运行 nightmare.js