如何使用 Python 在 Selenium 中以编程方式使 Firefox 无头?
How to make Firefox headless programmatically in Selenium with Python?
我 运行 此代码使用 python、selenium 和 firefox,但仍然获得 'head' 版本的 firefox:
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")
要无头调用 Firefox 浏览器,您可以通过 Options()
class 设置 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()
还有另一种方法可以实现无头模式。如果你需要在 Firefox 中禁用或启用无头模式,而不更改代码,你可以将环境变量 MOZ_HEADLESS
设置为 whatever 如果你想让 Firefox 运行 ] 无头,或者根本不设置。
这在您使用持续集成时非常有用,并且您希望 运行 服务器中的功能测试但仍然能够 运行 在您的 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 视频的步骤
- Mozilla Firefox in Headless Mode through Selenium 3.5.2 (Java)
- Login into Gmail Account using Headless Chrome through Selenium Java
结尾
我的回答:
set_headless(headless=True) is deprecated.
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')
已测试,按预期工作,这是来自 Official - Headless Mode | Mozilla
我 运行 此代码使用 python、selenium 和 firefox,但仍然获得 'head' 版本的 firefox:
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")
要无头调用 Firefox 浏览器,您可以通过 Options()
class 设置 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()
还有另一种方法可以实现无头模式。如果你需要在 Firefox 中禁用或启用无头模式,而不更改代码,你可以将环境变量 MOZ_HEADLESS
设置为 whatever 如果你想让 Firefox 运行 ] 无头,或者根本不设置。
这在您使用持续集成时非常有用,并且您希望 运行 服务器中的功能测试但仍然能够 运行 在您的 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 视频的步骤
- Mozilla Firefox in Headless Mode through Selenium 3.5.2 (Java)
- Login into Gmail Account using Headless Chrome through Selenium Java
结尾
我的回答:
set_headless(headless=True) is deprecated.
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')
已测试,按预期工作,这是来自 Official - Headless Mode | Mozilla