Diego Baggio
Diego Baggio

Reputation: 69

How to keep Selenium webdriver minimized throughout the process?

I developed a bot to download some files in the background, while working. But every time a download is made, the webdriver screen appears on the screen and I have to manually minimize it. Any suggestion?

options = Options()

options.add_experimental_option("prefs", {
            "plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}],
           "download.default_directory":"D:\Download", 
           "download.extensions_to_open": "applications/pdf",
           "download.prompt_for_download": False,
           "download.directory_upgrade": True,
           "plugins.always_open_pdf_externally": True,
           "safebrowsing_for_trusted_sources_enabled": False,
           "safebrowsing.enabled": False,
           "profile.default_content_setting_values.notifications" : 2
           })

driver = webdriver.Chrome('chromedriver',chrome_options=options)
driver.get("https://www.copel.com/AgenciaWeb/autenticar/loginCliente.do")

headless is not a option in my case

Upvotes: 0

Views: 2478

Answers (1)

Devansh Soni
Devansh Soni

Reputation: 771

As mentioned here: Selenium Web-driver Documentation

You can minimize or maximize your browser window using minimize_window() and maximize_window() functions.

Adding this line solves your problem:

driver.minimize_window()

But only adding this may not help if your website opens a new tab after clicking on some link. So, adding these lines will ensure that your script is working with last opened tab and not the first tab. Add these lines in your script after every click event which results in opening a new tab.

win_list = driver.window_handles
driver.switch_to.window(win_list[-1])

For example, Here, l1 will throw Error if we don't switch to last opened tab.

driver.get("https://sites.google.com/a/chromium.org/chromedriver/home")

l0 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div/div[2]/div/table/tbody/tr/td[2]/div/div[3]/div/table/tbody/tr/td/div/h2/font/a')
l0.click()  # This click event results in opening a new tab

win_list = driver.window_handles # This gives the list of all tabs
driver.switch_to.window(win_list[-1]) # To switch to the last tab opened

l1 = driver.find_element_by_xpath('/html/body/div[2]/div/div[1]/div/div[2]/div/table/tbody/tr/td[1]/div/div/ul/li[2]/div/a')
l1.click()

Upvotes: 2

Related Questions