如何使用python在Selenium中以编程方式使firefox无头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46753393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:49:28  来源:igfitidea点击:

How to make firefox headless programmatically in Selenium with python?

pythonpython-3.xseleniumselenium-webdriverfirefox-headless

提问by Tintinabulator Zea

I am running this code with python, selenium, and firefox but still get 'head' version of firefox:

我正在使用 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)

I also tried some variations of binary:

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

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

回答by DebanjanB

To invoke Firefox Browser headlessly, you can set the headlessproperty through Options()class as follows:

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

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


There's another way to accomplish headless mode. If you need to disable or enable the headless mode in Firefox, without changing the code, you can set the environment variable MOZ_HEADLESSto whateverif you want Firefox to run headless, or don't set it at all.

还有另一种方法可以实现无头模式。如果你需要禁用或启用Firefox中的无头模式,而无需修改代码,您可以设置环境变量MOZ_HEADLESS,以什么,如果你想Firefox的运行无头,或根本不设置它。

This is very useful when you are using for example continuous integration and you want to run the functional tests in the server but still be able to run the tests in normal mode in your PC.

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

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

or

或者

$ 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


Outro

奥特罗

How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?

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

回答by Philippe Delteil

The first answer does't work anymore.

第一个答案不再有效。

This worked for me:

这对我有用:

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

回答by TomasH

Just a note for people who may have found this later (and want java way of achieving this); FirefoxOptionsis also capable of enabling the headless mode:

只是给那些可能后来发现这个的人的注释(并希望以 java 方式实现这一点);FirefoxOptions还能够启用无头模式:

FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setHeadless(true);