简体   繁体   English

selenium webdriver.Firefox 无法为非 sudo 用户启动

[英]selenium webdriver.Firefox not starting for non-sudo user

I have a python method that sets up a browser in headless-mode on a linux server for website scraping with selenium.我有一个 python 方法,可以在 linux 服务器上以无头模式设置浏览器,以便使用 selenium 进行网站抓取。 The display gets setup perfectly fine regardless of which user executes the python script but if the sudo user doesn't execute the script it will hang at the webdriver.Firefox() setup line indefinitely.无论哪个用户执行 python 脚本,显示设置都非常好,但如果sudo用户不执行脚本,它将无限期地挂在webdriver.Firefox()设置行上。

Here is the full method:这是完整的方法:

def browserSetup(self, browser=None):
    try:
        # now Firefox will run in a virtual display. you will not see the browser.  
        self.display = Display(visible=0, size=(800, 600))
        self.display.start()

        if self.verbose:
            print "Virtual display started for browser instantiation."

        #change user agent
        profile = webdriver.FirefoxProfile()
        profile.set_preference("general.useragent.override", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/6.0.5 Safari/536.30.1")
        profile.set_preference("webdriver.log.file", "webdriver.log")

        # Create a new instance of the Firefox driver
        browser = webdriver.Firefox(profile)

        if self.verbose:
            print "Browser window object established @ %s." % browser

        return browser
    except Exception, e:
        raise e

So, to repeat my issue: if the script is not executed as sudo then the script will hang indefinitely at the webdriver.Firefox creation line.所以,重复我的问题:如果脚本不是作为sudo执行的,那么脚本将无限期地挂在webdriver.Firefox创建行。 Why would this be happening?为什么会发生这种情况?

UPDATE: The problem is this line here:更新:问题是这里的这一行:

 browser = webdriver.Firefox() #with or without the profile variable - the results are the same

UPDATE AGAIN Several people in the comments below have suggested I try running Firefox from the command line manually to see if there are any issues;再次更新下面评论中的几个人建议我尝试从命令行手动运行Firefox以查看是否有任何问题; here are the results:结果如下:

#initialize the virtual display
$ sudo Xvfb :10 -extension RANDR
[dix] Could not init font path element /usr/share/fonts/X11/cyrillic, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/100dpi/:unscaled, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/75dpi/:unscaled, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/Type1, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/100dpi, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/75dpi, removing from list!
[dix] Could not init font path element /var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType, removing from list!

#now start firefox in another ssh window (since the Xvfb process is consuming my prompt)
$ export DISPLAY=:10
$ firefox

(firefox:6347): GConf-WARNING **: Client failed to connect to the D-BUS daemon:
//bin/dbus-launch terminated abnormally without any error message

The last error message displays hundreds of times...最后一条错误信息显示数百次...

I can only guess, but here's what I suspect.我只能猜测,但这是我怀疑的。

sudo clears your environment variables when you start Firefox via it.当您通过它启动 Firefox 时, sudo清除您的环境变量。 That includes the DISPLAY variable as well.这也包括DISPLAY变量。

Two ways to disable this behavior: - Disable env_reset in your sudoers configuration.禁用此行为的两种方法: - 在 sudoers 配置中禁用env_reset - Use sudo -i , that will preserve the value of DISPLAY . - 使用sudo -i ,这将保留DISPLAY的值。

First you need to start the Xvfb (virtual frame-buffer X server) in the background.首先,您需要在后台启动Xvfb (虚拟帧缓冲 X 服务器)。

For example, $ sudo Xvfb :1 & or $ sudo Xvfb :1 -screen 0 1280x1024x8 .例如, $ sudo Xvfb :1 &$ sudo Xvfb :1 -screen 0 1280x1024x8

Then, whenever you want to run your script, do it with the DISPLAY environment variable set accordingly.然后,每当您想运行脚本时,请使用相应设置的 DISPLAY 环境变量来执行。

For example, $ DISPLAY=:1 python your_script.py .例如, $ DISPLAY=:1 python your_script.py

You may not want to open usr/lib up for good but try testing it with open perms on the directory.您可能不想永久打开 usr/lib,但尝试在目录上使用 open perms 对其进行测试。

sudo chmod 755 /usr/lib

If that's the issue, you can always move the drivers inside your application.如果这是问题所在,您始终可以在应用程序中移动驱动程序。 Unless you're sharing them with many people, they're small enough to have several copies.除非您与许多人共享它们,否则它们小到可以有多个副本。

For anyone else like me that until today could be having this problem: For me was due to Firefox needs to be able to create a profile, which is created at the user home folder.对于像我这样的人,直到今天都可能遇到这个问题:对我来说,是因为 Firefox 需要能够创建一个配置文件,该配置文件是在用户主文件夹中创建的。 The user i created didn't had its own user home folder so when i created it the problem was solved.我创建的用户没有自己的用户主文件夹,所以当我创建它时,问题就解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM