1

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.

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270

1 Answers1

1

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

Community
  • 1
  • 1
dm03514
  • 54,664
  • 18
  • 108
  • 145
  • Aha! Yes, I see — in my own home directory, I’ve got `.gnome2` and `.gnome2_private` directories. I didn’t realise `www-data`’s home directory was `/var/www`. I guess it doesn’t have write permissions there by default because that’s where you’d store documents to be served by Apache. The solution I’ve picked is to manually create `.gnome2` and `.gnome2_private` directories in `/var/www`, and make them be owned by the `www-data` user and group. That seems to allow PyVirtualDisplay to work. – Paul D. Waite Oct 03 '12 at 08:31
  • (I’m not sure if there’s any changes I should make to my Apache config file to ensure that the `.gnome2` and `.gnome2_private` folders aren’t visible on the web.) – Paul D. Waite Oct 03 '12 at 09:17