简体   繁体   中英

Get browser window size using Selenium Python

I am using selenium to get the browser's window size. Which is not the same as the window size.

In JS, we can do this by

driver.manage().window().getSize();

In Python, all I could find was

driver.get_window_size()

But this gives the window size, which is not the same as the browser size.

How to get the browser window size in selenium python?

get_window_size()

get_window_size() gets the width and height of the current window.

This method is defined as:

def get_window_size(self, windowHandle: str = 'current') -> dict:
    """
    Gets the width and height of the current window.

    :Usage:
        ::

            driver.get_window_size()
    """

    if windowHandle != 'current':
        warnings.warn("Only 'current' window is supported for W3C compatible browsers.")
    size = self.get_window_rect()

    if size.get('value', None):
        size = size['value']

    return {k: size[k] for k in ('width', 'height')}

and still seems to work perfecto .

  • Sample code block:

     driver.execute("get", {'url': 'https://www.google.com/'}) print(driver.get_window_size()) driver.maximize_window() print(driver.get_window_size())
  • Console output:

     {'width': 1050, 'height': 708} {'width': 1382, 'height': 744}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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