簡體   English   中英

在Linux中使用Selenium運行無頭Firefox

[英]Running headless firefox with selenium in Linux

我正在嘗試在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位版本存在問題。 通常打開並掛在那里什么也不做。

如果那是您的問題,如果您的測試不是基於壁虎驅動程序,請獲取45ff之前的32位版本。 對於ff45 +,還要獲取壁虎驅動程序並將二進制文件添加到(壁虎驅動程序的)路徑中。 然后,您應該以這種方式使用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上有一個與此相關的線程。 完全不需要

干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM