如何在 Xvfb 中运行 Selenium?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6183276/
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-08-04 00:59:15  来源:igfitidea点击:

How do I run Selenium in Xvfb?

pythonlinuxuser-interfaceunixselenium

提问by TIMEX

I'm on EC2 instance. So there is no GUI.

我在 EC2 实例上。所以没有GUI。

$pip install selenium
$sudo apt-get install firefox xvfb

Then I do this:

然后我这样做:

$Xvfb :1 -screen 0 1024x768x24 2>&1 >/dev/null &

$DISPLAY=:1 java -jar selenium-server-standalone-2.0b3.jar
05:08:31.227 INFO - Java: Sun Microsystems Inc. 19.0-b09
05:08:31.229 INFO - OS: Linux 2.6.32-305-ec2 i386
05:08:31.233 INFO - v2.0 [b3], with Core v2.0 [b3]
05:08:32.121 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
05:08:32.122 INFO - Version Jetty/5.1.x
05:08:32.123 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
05:08:32.124 INFO - Started HttpContext[/selenium-server,/selenium-server]
05:08:32.124 INFO - Started HttpContext[/,/]
05:08:32.291 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@1186fab
05:08:32.292 INFO - Started HttpContext[/wd,/wd]
05:08:32.295 INFO - Started SocketListener on 0.0.0.0:4444
05:08:32.295 INFO - Started org.openqa.jetty.jetty.Server@1ffb8dc

Great, everything should work now, right?

太好了,现在应该一切正常了,对吧?

When I run my code:

当我运行我的代码时:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox() 
browser.get("http://www.yahoo.com") 

I get this:

我明白了:

Error: cannot open display: :0

采纳答案by A.J

open a terminal and run this command xhost +. This commands needs to be run every time you restart your machine. If everything works fine may be you can add this to startup commands

打开终端并运行此命令 xhost +。每次重新启动机器时都需要运行此命令。如果一切正常,您可以将其添加到启动命令中

Also make sure in your /etc/environment file there is a line

还要确保在您的 /etc/environment 文件中有一行

export DISPLAY=:0.0 

And then, run your tests to see if your issue is resolved.

然后,运行您的测试以查看您的问题是否已解决。

All please note the comment from sardathrion below before using this.

在使用此之前,所有请注意下面来自 sardathrion 的评论。

回答by shang

This is the setup I use:

这是我使用的设置:

Before running the tests, execute:

在运行测试之前,执行:

export DISPLAY=:99
/etc/init.d/xvfb start

And after the tests:

在测试之后:

/etc/init.d/xvfb stop

The init.dfile I use looks like this:

init.d我使用的文件如下所示:

#!/bin/bash

XVFB=/usr/bin/Xvfb
XVFBARGS="$DISPLAY -ac -screen 0 1024x768x16"
PIDFILE=${HOME}/xvfb_${DISPLAY:1}.pid
case "" in
  start)
    echo -n "Starting virtual X frame buffer: Xvfb"
    /sbin/start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS
    echo "."
    ;;
  stop)
    echo -n "Stopping virtual X frame buffer: Xvfb"
    /sbin/start-stop-daemon --stop --quiet --pidfile $PIDFILE
    echo "."
    ;;
  restart)
    
#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

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

# now Firefox will run in a virtual display. 
# you will not see the browser.
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.quit()

display.stop()
stop
from xvfbwrapper import Xvfb

vdisplay = Xvfb()
vdisplay.start()

# launch stuff inside virtual display here

vdisplay.stop()
start ;; *) echo "Usage: /etc/init.d/xvfb {start|stop|restart}" exit 1 esac exit 0

回答by Corey Goldberg

You can use PyVirtualDisplay(a Python wrapper for Xvfb) to run headless WebDriver tests.

您可以使用PyVirtualDisplay(Xvfb 的 Python 包装器)来运行无头 WebDriver 测试。

from xvfbwrapper import Xvfb

with Xvfb() as xvfb:
    # launch stuff inside virtual display here.
    # It starts/stops in this code block.

more info

更多信息



You can also use xvfbwrapper, which is a similar module (but has no external dependencies):

您还可以使用xvfbwrapper,它是一个类似的模块(但没有外部依赖项):

DISPLAY=:1 xvfb-run java -jar selenium-server-standalone-2.0b3.jar

or better yet, use it as a context manager:

或者更好的是,将其用作上下文管理器:

##代码##

回答by ema

The easiest way is probably to use xvfb-run:

最简单的方法可能是使用 xvfb-run:

##代码##

xvfb-run does the whole X authority dance for you, give it a try!

xvfb-run 为你做全X权威舞,试试看!

回答by Toilal

If you use Maven, you can use xvfb-maven-pluginto start xvfb before tests, run them using related DISPLAYenvironment variable, and stop xvfb after all.

如果你使用Maven,你可以在测试前使用xvfb-maven-plugin启动xvfb,使用相关的DISPLAY环境变量运行它们,然后停止xvfb。