Java 如何使用 Selenium Webdriver 获取当前窗口大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35697931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:01:13  来源:igfitidea点击:

How to get the Current window Size using Selenium Webdriver?

javaseleniumselenium-webdriver

提问by Nikhil Surendran

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:

我在 (1366 X 768 ) 的屏幕分辨率上运行,但是当我调用getSize().getWidth()getSize().getHeight()方法时,我得到的结果是:

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

宽度尺寸为:1382 高度尺寸为:744

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

适用于 IE、FF 和 Chrome 网页。使用的网址:https: //www.google.com

回答by Guy

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

数字电视、计算机显示器或显示设备的显示分辨率或显示模式是每个维度上可以显示的不同像素的数量……“1024 × 768”表示宽度为 1024 像素,高度为 768 像素

While getSize()is returning the actual browsersize.

WhilegetSize()正在返回实际的浏览器大小。

回答by murali selenium

As we know Selenium interacts with browsers and these getmethods 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.

正如我们所知,Selenium 与浏览器交互,这些get方法只会检索与浏览器相关的信息。正如其他答案中非常清楚地解释的那样,屏幕分辨率和浏览器是不同的。下面的简单示例非常清楚地表明 Web 驱动程序仅获取浏览器的尺寸。

    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)

回答by Eyal Sooliman

    Dimension initial_size = driver.manage().window().getSize();
    int height = initial_size.getHeight();
    int width = initial_size.getWidth();

回答by Andriy Ivaneyko

For pythonselenium webdriver use function get_window_size:

对于pythonselenium webdriver 使用函数get_window_size

driver.get_window_size()
"""
Output:
{
  "width": 1255,
  "height": 847,
  "hCode": 939524096,
  "class": "org.openqa.selenium.Dimension"
}
"""