java Selenium 获取元素的自然高度和宽度。不应依赖样式属性。GetSize()、GetLocation() 和 getRect() 无法执行此操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36285374/
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-11-03 01:12:28  来源:igfitidea点击:

Selenium get natural height and width of an element. should not rely on style attribute. GetSize(), GetLocation() and getRect() fails to do so

javaseleniumselenium-webdriver

提问by Maniraj Mkr

Here is the scenario.

这是场景。

When am using GetSize(), GetLocation() Functions against the image ID 'FlashID1x' its always giving 250,300 but the actual height and width of an element is 1 X 1 which is basically wrong.

当我使用 GetSize() 时,GetLocation() 函数针对图像 ID 'FlashID1x' 总是给出 250,300,但元素的实际高度和宽度是 1 X 1,这基本上是错误的。

Here is my target dom:

这是我的目标 dom:

<img id="FlashID1x" border="0" width="300" height="250" style="width:300px;height:250px;" alt="" src="http://s2.adform.net/Banners/invisible.gif?bv=2"/>

Here is my code:

这是我的代码:

System.out.println("total : "+iframe.size());  
//driver.switchTo().frame(frame);

org.openqa.selenium.Point point=driver.findElement(By.xpath(".//*[@id='FlashID1x']")).getLocation();  
System.out.println("X Position : "+point.x);  
System.out.println("Y Position : "+point.y);  

System.out.println("X getX : "+point.getX());  
System.out.println("Y gety : "+point.getY());  

Rectangle pointer=driver.findElement(By.xpath(".//*[@id='FlashID1x']")).getRect();
System.out.println("height : "+pointer.hashCode();
System.out.println(" width : "+pointer.getWidth());  

System.out.println("getHeight : "+pointer.getHeight());  
System.out.println(" getWidth : "+pointer.getWidth());  

回答by Florent B.

The getSize method returns the rendered web element size and not the physical size of an image. If your goal is to get the intrinsic heigh and weight, then you could try to get the naturalWidth and naturalHeight properties:

getSize 方法返回呈现的 Web 元素大小,而不是图像的物理大小。如果您的目标是获得固有的高度和重量,那么您可以尝试获得 naturalWidth 和 naturalHeight 属性:

WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 20);
driver.get("http://stackoverflow.com");

// get the intrinsic size with the getAttribute method
WebElement ele = driver.findElement(By.cssSelector("img"));
String naturalWidth = ele.getAttribute("naturalWidth");
String naturalHeight = ele.getAttribute("naturalHeight");

// get the intrinsic size with a piece of Javascript
ArrayList result = (ArrayList)((JavascriptExecutor) driver).executeScript(
        "return [arguments[0].naturalWidth, arguments[0].naturalHeight];", ele);
Long naturalWidth2 = (Long)result.get(0);
Long naturalHeight2 = (Long)result.get(1);

回答by Andrew Regan

Irrespective of the size of the image itself, it's pretty clear from the WebDriver specthat it's CSS that determines the <img>element's dimensions, the same rule applied to every other type of element.

无论图像本身的大小如何,从WebDriver 规范中可以清楚地看出,决定<img>元素尺寸的是 CSS,同样的规则适用于所有其他类型的元素。

I think that's fair enough. Your markup clearly doesn't match the underlying image, but presumably there's some justification for that we can't see. Since your element's inflated size has an impact on the positioning of other elements, it's only fair that WebDriver accurately reports theirlocations, rather than pretending that they're neatly tucked alongside a tiny square.

我认为这很公平。您的标记显然与底层图像不匹配,但大概有一些我们看不到的理由。由于元素的膨胀大小会影响其他元素的位置,因此 WebDriver 准确报告它们的位置是公平的,而不是假装它们整齐地塞在一个小方块旁边。

If you need workarounds: you could create a getImageRect()method with a special override for that URL pattern or Id, you could use an HTTP library to check the real image's size on startup and cache it, or you could simply hardcode 1x1.

如果您需要变通方法:您可以getImageRect()为该 URL 模式或 Id创建一个具有特殊覆盖的方法,您可以使用 HTTP 库在启动时检查真实图像的大小并缓存它,或者您可以简单地硬编码 1x1。