如何解决硒得到元素的自然高度和宽度不应该依赖样式属性GetSize,GetLocation和getRect无法这样做
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);
解决方法
这是场景。
当使用GetSize()时,GetLocation()针对图像ID“ FlashID1x”起作用,它始终给出250,300,但元素的实际高度和宽度为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"/>
这是我的代码:
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());
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。