Running headless firefox with selenium in Linux

I am trying to run a headless Firefox browser on Linux. I have firefox installed and on my PATH, xvfb is installed, and am using pyvirtualdisplay to setup the display with 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. 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. Yes, there are issues with 64bit version in Linux, based on my experience. 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. For ff45+ get also the gecko driver and add the binary to path(of gecko driver). Then you should use Firefox Binary this way.

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. On linux you have to make the binary portable, there is a thread, here on Stack, about that. Altough is not necesary

Cheers

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

上一篇: 无法连接到MozRepl / RemoteObject.pm

下一篇: 在Linux中运行selenium的无头火狐