如何使用Webdriver Selenium获取“style”元素的值
我想检查样式元素的值是否大于特定值(即,是> 666px?),但我无法得到该值。
以下是我要捕获的样式的HTML代码:
我正在使用此代码尝试打印其值,但它不打印:
System.out.print(driver.findElement(By.id("da1c")).findElement(By.cssSelector("span")).getAttribute("style"));
我想要这样的东西:
if ((driver.findElement(By.id("da1c")).findElement(By.cssSelector("span")).getAttribute("style")).value> 700) { System.out.println("value exceeding") }
您可以捕获Computed Css值,如下面的firebug屏幕截图所示:
喜欢这个:
WebDriver web = new FirefoxDriver(; String visibility = web.findElement(By.xpath("//your xpath")).getCssValue("display");
如果在该范围内执行
.getAttribute("style")
,您将收到一个String。
left: 666px; top: 27px;
您可以使用字符串操作来获取特定样式。或者,您可以使用JavaScriptExecutor执行一些javascript魔术,并直接获取
left
值String script = "var thing = window.document.getElementById('da2c'); window.document.defaultView.getComputedStyle(thing, null).getPropertyValue('left');";
然后从那里检查它。
driver.findElement(By.locator( yourLocator )).getAttribute( requiredAttribute )
它将返回String