Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

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

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

Then I did 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 [email protected]
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 [email protected]

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

1 Answer

0 votes
by (62.9k points)

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.d file I use looks like this:

 

#!/bin/bash

XVFB=/usr/bin/Xvfb

XVFBARGS="$DISPLAY -ac -screen 0 1024x768x16"

PIDFILE=${HOME}/xvfb_${DISPLAY:1}.pid

case "$1" 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)

    $0 stop

    $0 start

    ;;

  *)

  echo "Usage: /etc/init.d/xvfb {start|stop|restart}"

  exit 1

esac

exit 0

Related questions

0 votes
1 answer
0 votes
1 answer
asked Apr 2, 2020 in AWS by chandra (29.3k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...