您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

selenium得到元素的自然高度和宽度。不应该依赖样式属性。GetSize(),GetLocation()和getRect()无法这样做

selenium得到元素的自然高度和宽度。不应该依赖样式属性。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);
其他 2022/1/1 18:21:40 有522人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶