Java 硒元素具有元素的自然高度和宽度。不应依赖于样式属性。GetSize()、GetLocation()和getRect()无法执行此操作

Java 硒元素具有元素的自然高度和宽度。不应依赖于样式属性。GetSize()、GetLocation()和getRect()无法执行此操作,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,下面是一个场景 当我使用GetSize()时,GetLocation()针对图像ID“FlashID1x”执行函数,它总是给出250300,但元素的实际高度和宽度是1 X 1,这基本上是错误的 这是我的目标dom: <img id="FlashID1x" border="0" width="300" height="250" style="width:300px;height:250px;" alt="" src="http://s2.adform.net/Banners/invisibl

下面是一个场景

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

这是我的目标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"/>

不管图像本身的大小如何,很明显,CSS决定了
元素的尺寸,同样的规则适用于所有其他类型的元素

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


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

getSize方法返回渲染的web元素大小,而不是图像的物理大小。 如果您的目标是获得固有的高度和重量,那么您可以尝试获得自然宽度和自然高度属性:

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);

为了澄清,您是在尝试获取图像本身的大小(1x1)还是包含图像的
img
元素的大小(300x250)?获取图像大小的最终目标是什么?
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);