Niko
Niko

Reputation: 61

How to un-minimize a browser window with selenium?

I've read through several minimization-related questions using Selenium but nowhere in their documentation says how to interface with a window that has been minimized. If you try running the set_window_size() or maximize_window() functions on the driver after minimizing the browser, Selenium throws an exception stating that the window state is invalid for these operations. I find it hard to believe that once you minimize the window you can no longer drive the browser. Has anyone done this?

EDIT: Here's my code:

self.driver.minimize_window()
self.driver.maximize_window()

And here's the traceback I'm seeing:

Traceback (most recent call last):
...
    self.browser.maximize_window()
  File "/home/user/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 737, in maximize_window
    self.execute(command, params)
  File "/home/user/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/user/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: failed to change window state to normal, current state is minimized
  (Session info: chrome=70.0.3538.77)
  (Driver info: chromedriver=2.41,platform=Linux 4.15.0-39-generic x86_64)

Upvotes: 5

Views: 2277

Answers (2)

Morteza Taghizadeh
Morteza Taghizadeh

Reputation: 2147

I had this problem too.

You have to store driver.current_window_handle as a variable before minimizing. Then use driver.switch_to.window to switch to that window.

Follow this link to reading more details: How to un-minimize selenium window?

Upvotes: 3

Niko
Niko

Reputation: 61

Found the answer while going through commit logs: https://github.com/w3c/webdriver/commit/1e1e590542da97f64cf58268940f88aec5ad6697

The solution is to call set_window_rect() on the driver. This will restore the state of the window after minimizing, maximizing, or going fullscreen.

EDIT: The bigger issue here is that the webdriver I'm using isn't w3c-compliant. The above will only work with w3c webdrivers.

Upvotes: 1

Related Questions