Saturday, April 25, 2015

Chrome - Maximise Window

You may have noticed that Chrome seems to ignore the maximise_window() command. Here is a simple solution.

self.driver = webdriver.Chrome()

if not self.driver.capabilities['browserName'] == 'Ie':
     screen_width  = self.driver.execute_script("return window.screen.availWidth;")
     screen_height = self.driver.execute_script("return window.screen.availHeight;")
     self.driver.set_window_size(screen_width, screen_height)
else:
     self.driver.maximize_window()

Not surprisingly, IE will baulk at this, hence the if block. It will get Chrome full-screening nicely however.

Consider the following scenario. You are preparing a Selenium script for a responsive site which in places has slightly different CSS selectors for desktop and devices. By hardcoding a value for screen_width, you can view the device HTM in your desktop browser and inspect the differing selectors (for devices) in devtools in the normal fashion.

No comments:

Post a Comment