如何在 Xvfb 中运行 Selenium? [英] How do I run Selenium in Xvfb?

查看:19
本文介绍了如何在 Xvfb 中运行 Selenium?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

然后我这样做:

$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?

当我运行我的代码时:

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

我明白了:

Error: cannot open display: :0

推荐答案

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

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

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

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

export DISPLAY=:0.0 

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

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

请大家在使用之前注意下面来自 sardathrion 的评论.

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

这篇关于如何在 Xvfb 中运行 Selenium?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆