Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run Selenium in Xvfb?

I'm on EC2 instance. So there is no 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 
like image 531
TIMEX Avatar asked May 31 '11 05:05

TIMEX


People also ask

What is XVFB selenium?

xvfb-run ./Runner. In this case, Runner is a console application that uses Selenium to run automated test scenarios in Firefox and Chrome. This is how you can make any GUI application headless. Yon can also run it in Docker: Run xvfb + Firefox in Docker. chromefirefoxlinuxhow toSeleniumxvfb.

What is XVFB process?

Xvfb (short for X virtual framebuffer) is an in-memory display server for UNIX-like operating system (e.g., Linux). It enables you to run graphical applications without a display (e.g., browser tests on a CI server) while also having the ability to take screenshots.


1 Answers

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

#!/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() 

more info


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

from xvfbwrapper import Xvfb  vdisplay = Xvfb() vdisplay.start()  # launch stuff inside virtual display here  vdisplay.stop() 

or better yet, use it as a context manager:

from xvfbwrapper import Xvfb  with Xvfb() as xvfb:     # launch stuff inside virtual display here.     # It starts/stops in this code block. 
like image 71
Corey Goldberg Avatar answered Oct 05 '22 23:10

Corey Goldberg