Search code examples
pythondjangoseleniumpyvirtualdisplay

Why do I get a gnome permissions error when I run Selenium using PyVirtualDisplay from a Django website?


I’m trying to use Python’s Selenium bindings to programmatically interact with websites, as part of a Django website.

As my Selenium code runs as part of a Django website, by default (if I understand correctly) there is no display available for the browser to use. I’m thus trying to use PyVirtualDisplay to start Xvfb before my Selenium code runs.

Here‘s my code:

from pyvirtualdisplay import Display
from selenium import webdriver

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

browser = webdriver.Firefox()

When I SSH into my server (running Debian Squeeze, Python 2.6.6, Selenium 2.25, PyVirtualDisplay 0.1.0), run the Python console as myself, and type in the code above, it works fine.

However, when I try to run that code from my Django site, or use su to run the Python console as www-data (which I believe is the user that Django runs as), I get the following error:

selenium.common.exceptions.WebDriverException: Message: 'The browser appears to have exited before we could connect. The output was: 
(process:2963): Gtk-WARNING **: Locale not supported by C library.
    Using the fallback 'C' locale.
    Xlib:  extension "RANDR" missing on display ":1082.0".

    (firefox-bin:2963): libgnomevfs-WARNING **: Unable to create ~/.gnome2 directory: Permission denied
    Could not create per-user gnome configuration directory `/var/www/.gnome2/\': Permission denied'

I’m a bit of a noob with Xvfb and Linux, so I’m not quite sure what I’m doing wrong.


Solution

  • I believe this is a simple permissions error.

    On ubuntu apaches home directory, as you see, is /var/www I think you just need to make sure that apache has write access to its home directory. My default on ubuntu 12.04 is

    daniel@daniel:/var/www$ ls -la
    total 12
    drwxr-xr-x  2 root root 4096 Sep 15 11:43 .
    drwxr-xr-x 14 root root 4096 Oct  2 08:54 ..
    -rw-r--r--  1 root root  177 Sep 15 11:43 index.html
    

    www-data does not have write access to its own home directory!

    Perhaps you could have www-data own the directory, or create an admin group that has write permission and add www-data to it?

    Some other threads on giving write access to /var/www https://superuser.com/questions/19318/how-can-i-give-write-access-of-a-folder-to-all-users-in-linux