2013-08-26 87 views
6

我想检查样式元素的值是否大于特定值(即,剩余> 666px?),但我无法获取预见价值。如何使用Webdriver Selenium来获取“样式”元素的值

这里是风格的HTML代码,我想捕捉:

<pre><span id="da2c" style="left: 666px; top: 27px;"></pre> 

我使用此代码尝试打印它的价值,但它不是打印:

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") 
} 

回答

7

如果您在该跨度上执行.getAttribute("style"),您将收到一个字符串。

left: 666px; top: 27px;
您可以使用字符串操作来获取特定的样式。

或者,您也可以执行使用JavaScript魔法JavaScriptExecutor,并通过

String script = "var thing = window.document.getElementById('da2c'); 
          window.document.defaultView.getComputedStyle(thing, null).getPropertyValue('left');"; 

直接获取的left值一些,然后从那里检查。

+0

感谢好友 将调查它 –

+0

你有没有得到anyw这里有这个@SohaibMaroof? – sircapsalot

9

您可以捕捉计算的CSS值显示在下面的萤火虫截图:

enter image description here

这样的:

WebDriver web = new FirefoxDriver(; 
String visibility = web.findElement(By.xpath("//your xpath")).getCssValue("display"); 
0
driver.findElement(By.locator(yourLocator)).getAttribute(requiredAttribute) 

它将返回字符串

相关问题