java 获取 GDK_BACKEND 与 debian 中的可用显示错误不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40789338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:28:48  来源:igfitidea点击:

Getting GDK_BACKEND does not match available displays error in debian

javafirefoxselenium-webdriverdebianheadless-browser

提问by hellWarrior

Actually i am trying to run a headless browser in remote debian server through selenium. I have firefox 46.0.1 installed in the server and i am using selenium 2.53.1 version.

实际上,我正在尝试通过 selenium 在远程 debian 服务器中运行无头浏览器。我在服务器中安装了 Firefox 46.0.1,我使用的是 selenium 2.53.1 版本。

Whenever i tried to run a given test i got the following error.

每当我尝试运行给定的测试时,都会出现以下错误。

org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(/usr/bin/firefox) on port 7055; process output follows: 
Error: GDK_BACKEND does not match available displays

I have instantiated firefox driver in my code like this:

我在我的代码中实例化了 Firefox 驱动程序,如下所示:

saDriver = new FirefoxDriver();

can anyone help?

有人可以帮忙吗?

回答by Basil Jose

I am not familiar with Java. However in Python this issue can be solved by the following method, this may help you

我不熟悉Java。但是在 Python 中这个问题可以通过以下方法解决,这可能对你有帮助

If it says Error: GDK_BACKEND does not match available displays then install pyvirtualdisplay:

如果显示错误:GDK_BACKEND 与可用显示器不匹配,则安装 pyvirtualdisplay:

pip install pyvirtualdisplay selenium

You might need xvfb too:

您可能也需要 xvfb:

sudo apt-get install xvfb

Then try adding this code:

然后尝试添加以下代码:

from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()

Full example:

完整示例:

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

browser = webdriver.Firefox()
browser.get('http://www.python.org')

browser.close()
display.stop()

回答by sola

You need to export the display in the shell that selenium server is running in otherwise it will not be able to open the browser.

您需要在运行 selenium 服务器的 shell 中导出显示,否则它将无法打开浏览器。

nohup sudo Xvfb: 10 - ac &
export DISPLAY=10