python - 为什么 selenium webdriver Firefox 不适用于非特权用户?

标签 python firefox ubuntu selenium webdriver

我正在尝试使用 selenium 在 python 的 Django View 中创建屏幕截图。如果我在 root 下启动它,selenium firefox webdriver 运行良好。但是,当我尝试使用非 super 用户运行它时,它会在尝试实例化驱动程序时卡住。 Django 是通过 apache www-data 用户调用的,所以它会遇到这个问题。

有什么方法可以让 selenium firefox webdriver 以非 root 身份运行?

在全新的 Ubuntu 14.04 安装中,我执行了以下操作

sudo apt-get install python-pip firefox xvfb
pip install selenium pyvirtualdisplay
useradd testuser

然后在 python shell 中:

from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
driver = webdriver.Firefox()
driver.get("http://askubuntu.com")
print driver.page_source.encode('utf-8')
driver.quit()
display.stop()

如果我以 root 身份登录 python,这工作正常,如果我使用 testuser 帐户,行 driver = webdriver.Firefox() 没有响应或错误。

如果您能就为什么会发生这种情况提出任何建议,我将不胜感激。

最佳答案

我在 Linux 上使用 Selenium + Firefox 时遇到了同样的问题。问题是 linux 的用户:要运行这些测试,Firefox 需要能够创建一个配置文件(Firefox 的配置文件)。此配置文件位于 user_home/.mozilla/firefox/profiles

所以在你的情况下,检查:

  • 允许此 linux 用户在自己家中书写
  • etc/passwd 中检查此用户是否有默认 shell,例如 /bin/bash
  • 在您的 webapp 所在的目录中:尝试 $ ls -larth:如果此目录中的所有文件都属于 root,您可以尝试更改权限在此文件夹上,以允许非根用户访问它(然后允许运行 Firefox + Selenium)。您还可以更改组的权限并将根用户和非根用户都添加到该组

关于python - 为什么 selenium webdriver Firefox 不适用于非特权用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25914877/

相关文章:

python - Numpy 数组仅显示唯一行

python - pyopengl 上的 gldrawpixels 没有绘制任何东西?

html - 是否有一个库可以抽象 Web Audio API 和 Mozilla Audio Data API 来读取原始音频(MP3,ogg)

php - 警告 : Unknown: Unable to create temporary file, 检查临时文件目录中的权限。在第 0 行的未知中

postgresql - 如何解决错误重复的sources.list?

python - LGB 范围规则和在 Python 中使用 "self"

javascript - 如何使用 JavaScript 在 Firefox 中将必需的 radio 输入设为可选?

html - 全屏背景 gif 在 Firefox 中不起作用

没有 RVM 的 Ruby + Ubuntu:执行 gem 时...(Gem::FilePermissionError)

python - 具有表示错误的四舍五入 float 到最接近和正确的结果