简体   繁体   English

在Linux中使用Selenium运行无头Firefox

[英]Running headless firefox with selenium in Linux

I am trying to run a headless Firefox browser on Linux. 我正在尝试在Linux上运行无头Firefox浏览器。 I have firefox installed and on my PATH, xvfb is installed, and am using pyvirtualdisplay to setup the display with xvfb. 我安装了firefox,并在PATH上安装了xvfb,并且正在使用pyvirtualdisplay设置xvfb的显示。 When the last line is executed 当最后一行执行时

from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=False, size=(1024, 768))
display.start()
browser = webdriver.Firefox()

I get the error message: 我收到错误消息:

WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details. 

I tried setting a log file as: 我尝试将日志文件设置为:

p = webdriver.FirefoxProfile()
p.set_preference("webdriver.firefox.logfile", "/tmp/firefox_log")
browser = webdriver.Firefox(p)

But there is no log file created (and creating the file first does not write to it). 但是,没有创建日志文件(并且首先创建该文件不会写入日志文件)。 How do you find out more information for what is going wrong? 您如何找到更多有关发生问题的信息? How do I fix this? 我该如何解决?

from pyvirtualdisplay import Display
from selenium import webdriver



class Firefox:

    def __init__(self):

        self.display = Display(visible=0, size=(800, 600))
        self.display.start()
        self.driver = webdriver.Firefox()
        self.driver.set_window_size(1120, 450)
    def shutdown(self):
        self.display.stop()
        self.driver.quit()

I think python is not pointed towards the correct binary. 我认为python没有指向正确的二进制文件。 I had some issues in the past like that after I deleted the binary that came with my distro and then installing the 64bit version. 在删除发行版随附的二进制文件然后安装64位版本之后,我曾经遇到过类似的问题。 Yes, there are issues with 64bit version in Linux, based on my experience. 是的,根据我的经验,Linux中的64位版本存在问题。 Usually opens and hangs there doing nothing. 通常打开并挂在那里什么也不做。

If that's your problem, get a 32bit prior to 45ff version if your testings are not based gecko driver. 如果那是您的问题,如果您的测试不是基于壁虎驱动程序,请获取45ff之前的32位版本。 For ff45+ get also the gecko driver and add the binary to path(of gecko driver). 对于ff45 +,还要获取壁虎驱动程序并将二进制文件添加到(壁虎驱动程序的)路径中。 Then you should use Firefox Binary this way. 然后,您应该以这种方式使用Firefox Binary。

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary("/usr/bin/firefox") #Or whatever path you have(E.G. Portable)
driver = webdriver.Firefox(firefox_binary=binary)

On windows I use portable apps. 在Windows上,我使用便携式应用程序。 On linux you have to make the binary portable, there is a thread, here on Stack, about that. 在Linux上,您必须使二进制文件具有可移植性,在Stack上有一个与此相关的线程。 Altough is not necesary 完全不需要

Cheers 干杯

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

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