简体   繁体   English

如何最小化或隐藏 Selenium 中的 Geckodriver?

[英]How to minimize or hide the Geckodriver in Selenium?

This is what I have:这就是我所拥有的:


from selenium import webdriver
driver = webdriver.Firefox()

How can I let the geckodriver open minimized or hidden?如何让geckodriver打开最小化或隐藏?

You have to set the firefox driver options to headless so that it opens minimized.您必须将 firefox 驱动程序选项设置为无头,以便最小化打开。 Here is the code to do that.这是执行此操作的代码。

fireFoxOptions = webdriver.FirefoxOptions()
fireFoxOptions.set_headless()
driver = webdriver.Firefox(firefox_options=fireFoxOptions)

If this doesn't work for you there are other methods that you can use.如果这对您不起作用,您可以使用其他方法。 Check out this other SO question for those: How to make firefox headless programmatically in Selenium with python?查看其他 SO 问题: How to make firefox headless in Selenium with python?

geckodriver壁虎司机

geckodriver in it's core form is a proxy for using W3C WebDriver-compatible clients to interact with Gecko-based browsers.geckodriver的核心形式是使用与 W3C WebDriver 兼容的客户端与基于 Gecko 的浏览器进行交互的代理

This program provides the HTTP API described by the WebDriver protocol to communicate with Gecko browsers, such as Firefox.本程序提供WebDriver协议描述的HTTP API与Gecko浏览器通信,如Firefox。 It translates calls into the Firefox remote protocol by acting as a proxy between the local- and remote ends.它通过充当本地端和远程端之间的代理,将调用转换为Firefox 远程协议

So more or less it acts like a service.所以或多或少它就像一个服务。 So the question to minimize the GeckoDriver shouldn't arise.所以不应该出现最小化GeckoDriver的问题。


Minimizing Mozilla Firefox browser最小化Mozilla Firefox浏览器

The Selenium driven GeckoDriver initiated Browsing Context by default opens in semi maximized mode. Selenium驱动的GeckoDriver启动浏览上下文默认以半最大化模式打开。 Perhaps while executing tests with the browsing context being minimized would be against all the best practices as Selenium may loose the focus over the Browsing Context and an exception may raise during the Test Execution .也许在最小化浏览上下文的情况下执行测试会违反所有最佳实践,因为Selenium可能会失去对浏览上下文的关注,并且在测试执行期间可能会引发异常。

However, Selenium's client does have a minimize_window() method which eventually minimizes the Chrome Browsing Context effectively.然而, Selenium 的客户端确实有一个minimize_window()方法,它最终有效地最小化了Chrome 浏览上下文


Solution解决方案

You can use the following solution:您可以使用以下解决方案:

  • Firefox : Firefox

     from selenium import webdriver options = webdriver.FirefoxOptions() options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe' driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe') driver.get('https://www.google.co.in') driver.minimize_window()
  • Chrome :

     from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument("--start-maximized") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get('https://www.google.co.in') driver.minimize_window()

Reference参考

You can find a detailed relevant discussion in:您可以在以下位置找到详细的相关讨论:


tl; tl; dr博士

How to make firefox headless programmatically in Selenium with python? 如何使用 python 在 Selenium 中以编程方式使 firefox 无头?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM