python - 当我从 Django 网站使用 PyVirtualDisplay 运行 Selenium 时,为什么会出现 gnome 权限错误?

标签 python django selenium pyvirtualdisplay

我正在尝试使用 Python 的 Selenium 绑定(bind)以编程方式与网站交互,作为 Django 网站的一部分。

由于我的 Selenium 代码作为 Django 网站的一部分运行,默认情况下(如果我理解正确的话)没有可供浏览器使用的显示。因此,我尝试在 Selenium 代码运行之前使用 PyVirtualDisplay 启动 Xvfb。

这是我的代码:

from pyvirtualdisplay import Display
from selenium import webdriver

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

browser = webdriver.Firefox()

当我通过 SSH 连接到我的服务器(运行 Debian Squeeze、Python 2.6.6、Selenium 2.25、PyVirtualDisplay 0.1.0)时,以我自己的身份运行 Python 控制台,然后输入上面的代码,它工作正常。

但是,当我尝试从 Django 站点运行该代码,或使用 suwww-data 运行 Python 控制台时(我相信这是用户Django 运行为),我收到以下错误:

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'

我对 Xvfb 和 Linux 有点菜鸟,所以我不太确定我做错了什么。

最佳答案

我认为这是一个简单的权限错误。

在 ubuntu apaches 主目录中,如您所见,是 /var/www 我认为你只需要确保 apache 对其主目录具有写访问权限。我在 ubuntu 12.04 上的默认设置是

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 对其自己的主目录没有写权限!

也许您可以让 www-data 拥有该目录,或者创建一个具有写入权限的管理组并将 www-data 添加到其中?

一些其他关于授予 /var/www 写入权限的线程 https://superuser.com/questions/19318/how-can-i-give-write-access-of-a-folder-to-all-users-in-linux

关于python - 当我从 Django 网站使用 PyVirtualDisplay 运行 Selenium 时,为什么会出现 gnome 权限错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12695091/

相关文章:

python - 如何在 python 中合并列表?

python - LinAlgError : Last 2 dimensions of the array must be square

javascript - 获得 200 ok 状态但给出 postman 的响应错误

javascript - JavaScript 中的 Django 模板实现?

python - 访问类的多处理代理的属性

python - 如何在 asyncio create_task 中更新全局变量

mysql - 如何在 Django 中确保 create_or_update 的唯一性而无需数据库级唯一约束

java - 令人沮丧的 ElementNotVisibleException 错误

windows - 在远程计算机上运行 QTP

python - 检查文本文件中的参数 Python