简体   繁体   English

Selenium Python 最小化浏览器窗口

[英]Selenium Python minimize browser window

I know how to call a method to maximize window from driver object.我知道如何调用一个方法来最大化驱动程序对象的窗口。

driver.maximize_window()

But what method should I use when I need to minimize browser window (hide it)?但是当我需要最小化浏览器窗口(隐藏它)时应该使用什么方法? Actually, driver object hasn't maximize_window attribute.实际上,驱动程序对象没有 Maximize_window 属性。 My goal to work silently with the browser window.我的目标是在浏览器窗口中静默工作。 I don't want to see it on my PC.我不想在我的电脑上看到它。

Option 1: Use driver.minimize_window()选项 1:使用 driver.minimize_window()

Option 2: Use --headless选项 2:使用 --headless

Example 1:示例 1:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=options)
driver.get("enter your url here")

Example 2:示例 2:

from selenium import webdriver

driver = webdriver.Chrome()
driver.minimize_window()
driver.get("enter your url here")

I would like to give a suggestion and make you correct that there's a method to minimize the window as you are required to do.我想提出一个建议并让您更正,有一种方法可以按照您的要求最小化窗口。

driver.minimize_window()

I would also like to mention that this will definitely work in python3, hope you were working in python.我还想提一下,这肯定会在 python3 中工作,希望你在 python 中工作。

Just driver.minimize_window() or use a headless browser as PhamtonJS or Chromium.只需driver.minimize_window()或使用无头浏览器作为 PhamtonJS 或 Chromium。

Example:例子:

PROXY_SERVER = "127.0.0.1:5566" # IP:PORT or HOST:PORT

options = webdriver.ChromeOptions()
options.binary_location = r"/usr/bin/opera" # path to opera executable
options.add_argument('--proxy-server=%s' % PROXY_SERVER)
driver = webdriver.Opera(executable_path=r"/home/prestes/Tools/operadriver_linux64/operadriver", options=options)
driver.minimize_window()
driver.get(url)

html = driver.page_source
soup = BeautifulSoup(html, "lxml")
print(html)

driver.quit()

Maybe try a headless browser?也许尝试无头浏览器? Chrome headless or PhantomJS. Chrome 无头或 PhantomJS。

http://phantomjs.org/ http://phantomjs.org/

Keep in mind that development is suspended for Phantom js.请记住,Phantom js 的开发已暂停。 You may want to use other alternatives if it doesn't work or gives errors -如果它不起作用或出现错误,您可能需要使用其他替代方法 -

https://github.com/dhamaniasad/HeadlessBrowsers https://github.com/dhamaniasad/HeadlessBrowsers

A headless browser is a web browser without a GUI.无头浏览器是没有 GUI 的 Web 浏览器。

尝试这个,

driver.set_window_position(0, 0)

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

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