How to get the current window size with Selenium Webdriver?
Asked Answered
I

6

13

I am running on Screen Resolution of (1366 X 768 ), but when I call getSize().getWidth() and getSize().getHeight() methods , the result I'm getting is:

Size of Width is : 1382 Size of Height is : 744

for IE, FF and Chrome web pages. URL used: https://www.google.com

Inunction answered 29/2, 2016 at 11:4 Comment(0)
K
12

As we know Selenium interacts with browsers and these get methods will retrieve info related to browsers only. As explained in other answers very clearly, screen resolution and browser are different. The simple example below shows very clearly that the web driver is getting only the browser's dimensions.

    WebDriver driver=new FirefoxDriver();
    driver.get("http://www.google.com");
    System.out.println(driver.manage().window().getSize()); //output: (994, 718)

    driver.manage().window().maximize();
    System.out.println(driver.manage().window().getSize()); //output: (1382, 744)
Kelcy answered 29/2, 2016 at 12:23 Comment(0)
L
8

For python selenium webdriver use function get_window_size:

driver.get_window_size()
"""
Output:
{
  "width": 1255,
  "height": 847,
  "hCode": 939524096,
  "class": "org.openqa.selenium.Dimension"
}
"""
Loadstone answered 19/12, 2016 at 10:10 Comment(0)
G
5

The screen resolution and browser window size are not necessarily equal.

Resolution (from wikipedia) is

The display resolution or display modes of a digital television, computer monitor or display device is the number of distinct pixels in each dimension that can be displayed ... "1024 × 768" means the width is 1024 pixels and the height is 768 pixels

While getSize() is returning the actual browser size.

Greenish answered 29/2, 2016 at 12:8 Comment(0)
G
5
    Dimension initial_size = driver.manage().window().getSize();
    int height = initial_size.getHeight();
    int width = initial_size.getWidth();
Georgia answered 26/10, 2016 at 15:36 Comment(1)
Hi Eyal Sooliman; your code might be correct, but with some context it would make a better answer; for example, you could explain how and why this proposed change would resolve the questioner's problem, perhaps including a link to the relevant documentation. That would make it more useful to them, and also more useful to other site readers who are looking for solutions to similar problems.Huge
S
0

try this:

from selenium import webdriver
driver = webdriver.Chrome('C/Program Files/Chromedriver') #Chromedriver location
print(driver.get_window_size())
Smollett answered 5/3, 2022 at 8:6 Comment(0)
D
0

In Python, you can use get_window_size() to get the current window size as shown below:

from selenium import webdriver

driver = webdriver.Chrome()
print(driver.get_window_size()) # {'width': 945, 'height': 1012}
print(driver.get_window_size().get('width')) # 945
print(driver.get_window_size().get('height')) # 1012
print(driver.get_window_size()['width']) # 945
print(driver.get_window_size()['height']) # 1012
Deportee answered 12/9, 2023 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.