在Linux中运行selenium的无头火狐

我正尝试在Linux上运行无头Firefox浏览器。 我安装了firefox,并在我的PATH上安装了xvfb,并使用pyvirtualdisplay以xvfb设置显示。 当最后一行被执行时

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

我收到错误消息:

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. 

我试着将日志文件设置为:

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

但是没有创建日志文件(并且首先创建文件不写入它)。 你如何找出更多有关错误的信息? 我该如何解决?


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()

我认为Python不是指向正确的二进制文件。 我删除了随发行版一起提供的二进制文件,然后安装了64位版本之后,我曾经遇到过一些问题。 是的,根据我的经验,Linux中有64位版本存在问题。 通常打开并挂在那里无所事事。

如果这是你的问题,如果你的测试不是基于Gecko驱动程序,那么在45ff版本之前获得32位。 对于ff45 +也得到gecko驱动程序并将二进制文件添加到路径(gecko驱动程序)。 那么你应该这样使用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)

在Windows上我使用便携式应用程序。 在linux上,你必须使二进制文件变得可移植,关于这个,在Stack上有一个线程。 Altough不是必需的

干杯

链接地址: http://www.djcxy.com/p/57523.html

上一篇: Running headless firefox with selenium in Linux

下一篇: Can't run firefox in headless mode