Solved Using pyvirtualdisplay - Python 2.7 / Selenium Web Driver

Below is the code am writing in FreeBSD. Before this code, I did pkg install xorg-vfbserver but after this I don't know which environment variable to set. Like in Ubuntu you have to do like this before using this program
apt-get install xvfb
export DISPLAY =:1


In FreeBSD, what is the environment variable to set ? I read USES = DISPLAY but I couldn't understand as there isn't much of information about it. And because of that the Firefox starts and closes
Code:
from pyvirtualdisplay importDisplay
from selenium import webdriver

try:
[INDENT]display =Display(visible=0, size=(800,600))
display.start()[/INDENT]
except:
[INDENT]print"no virtual display found"[/INDENT]


driver = webdriver.Firefox()

driver.get('www.google.com')

driver.close()

display.open.terminate()
 
Last edited:
It's still $DISPLAY. What shell are you using and what is the exact error you see? If you are using the default tcsh(1) then the syntax is setenv DISPLAY :0. If you are using a Bourne like shell then the syntax you had earlier is correct.
 
Hi junovitch@, thanks for the reply. Below are the steps I had to follow

To setup X Virtual Frame Buffer
sudo pkg install xorg-vfbserver
sudo pkg install xkeyboard-config
sudo pkg install xkbcomp
sudo pkg install x11-fonts/xorg-fonts


To start virtual frame buffer on display :1, you use below command. You can also set it in your rc file to start at boot
nohup Xvfb :1 -screen 0 800x600x16 > /dev/null 2>&1
Observe, I choose display :1 and therefore, it is this value to be set in the environment variable, which I will mention below

Install firefox
sudo pkg install firefox
If you don't create machine-id in /etc using dbus-uuidgen, your firefox would crash the moment it starts
sudo dbus-uuidgen > machine-id
sudo mv machine-id /etc/


Check your SHELL value
echo $SHELL

If it is /bin/tcsh then use below command
setenv DISPLAY :1
Note :1 because in Xvfb also I used :1

Now you don't even need pyvirtualdisplay and you don't have to initialize display in python. Just use selenium library and webdriver as below
Code:
from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
driver.get('www.google.com')

#please use WebDriverWait instead of sleep. I am using sleep just for this example
sleep(2)

driver.close()
 
Back
Top