如何用 Python 在 Selenium 中通过编程使 Firefox 无头?

我使用 python、 selenium 和 firefox 运行这段代码,但仍然得到了 firefox 的“ head”版本:

binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe', log_file=sys.stdout)
binary.add_command_line_options('-headless')
self.driver = webdriver.Firefox(firefox_binary=binary)

我还尝试了一些二进制的变体:

binary = FirefoxBinary('C:\\Program Files\\Nightly\\firefox.exe', log_file=sys.stdout)
binary.add_command_line_options("--headless")
201578 次浏览

要无头调用 Firefox 浏览器,可以通过 Options()类设置 headless属性如下:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options


options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://google.com/")
print ("Headless Firefox Initialized")
driver.quit()

还有一种方法可以完成无头模式。如果你需要在火狐中禁用或启用无头模式,而不改变代码,你可以将环境变量 MOZ_HEADLESS设置为 随便啦,如果你想让火狐运行无头模式,或者根本不设置。

当您正在使用例如持续集成,并且希望在服务器中运行功能测试,但是仍然能够在您的 PC 中以正常模式运行测试时,这非常有用。

$ MOZ_HEADLESS=1 python manage.py test # testing example in Django with headless Firefox

或者

$ export MOZ_HEADLESS=1   # this way you only have to set it once
$ python manage.py test functional/tests/directory
$ unset MOZ_HEADLESS      # if you want to disable headless mode

通过 YouTube 视频的步骤


外面

如何配置 ChromeDriver 通过 Selenium 在 Headless 模式下启动 Chrome 浏览器?

我的回答是:

set_headless(headless=True) is deprecated.

Https://seleniumhq.github.io/selenium/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.options.html

options.headless = True

我没问题

第一个答案已经不管用了。

这对我很有效:

from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium import webdriver


options = FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
driver.get("http://google.com")
Used below code to set driver type based on need of Headless / Head for both Firefox and chrome:


// Can pass browser type


if brower.lower() == 'chrome':
driver = webdriver.Chrome('..\drivers\chromedriver')
elif brower.lower() == 'headless chrome':
ch_Options = Options()
ch_Options.add_argument('--headless')
ch_Options.add_argument("--disable-gpu")
driver = webdriver.Chrome('..\drivers\chromedriver',options=ch_Options)
elif brower.lower() == 'firefox':
driver = webdriver.Firefox(executable_path=r'..\drivers\geckodriver.exe')
elif brower.lower() == 'headless firefox':
ff_option = FFOption()
ff_option.add_argument('--headless')
ff_option.add_argument("--disable-gpu")
driver = webdriver.Firefox(executable_path=r'..\drivers\geckodriver.exe', options=ff_option)
elif brower.lower() == 'ie':
driver = webdriver.Ie('..\drivers\IEDriverServer')
else:
raise Exception('Invalid Browser Type')

对于 OP 或者当前感兴趣的任何人,下面是我当前使用 Firefox 的代码部分:

opt = webdriver.FirefoxOptions()
opt.add_argument('-headless')
ffox_driver = webdriver.Firefox(executable_path='\path\to\geckodriver', options=opt)
from selenium.webdriver.firefox.options import Options


if __name__ == "__main__":
options = Options()
options.add_argument('-headless')
driver = Firefox(executable_path='geckodriver', firefox_options=options)
wait = WebDriverWait(driver, timeout=10)
driver.get('http://www.google.com')

测试,工程如预期,这是从 官方无头模式 | Mozilla

现在用这个代码:

options                 = Options()
options.headless        = True
driver                  = webdriver.Firefox(executable_path=GeckoDriverManager().install(),options=options)

我们有一个警告:

警告: 可执行路径已被弃用,请传入一个服务对象

换成这个,效果非常好:

#!/usr/bin/env python
# -*- coding: utf-8 -*-


# selenium drivers: https://www.selenium.dev/documentation/webdriver/getting_started/install_drivers/
# pip3 install selenium
# pip3 install webdriver-manager
# for custom firefox installation: link firefox to /usr/bin/firefox, example: ln -s /opt/firefox/firefox-bin /usr/bin/firefox


from selenium                           import webdriver
from webdriver_manager.firefox          import GeckoDriverManager
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service


options                 = Options()
options.headless        = True
service                 = Service(executable_path=GeckoDriverManager().install())
driver                  = webdriver.Firefox(service=service, options=options)


driver.get("http://google.com/")
print("Headless Firefox Initialized")
driver.quit()