Sohaib Maroof
Sohaib Maroof

Reputation: 131

How to use Webdriver Selenium to get the value of the "style" element

I want to check whether the value of a style element is greater than a particular value (i.e. is left > 666px ?), but I am unable to get ahold of the value.

Here is the HTML code of the style that I want to capture:

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

I am using this code to try to print its value, but it's not printing:

System.out.print(driver
      .findElement(By.id("da1c"))
      .findElement(By.cssSelector("span"))
      .getAttribute("style")
);

I want something like this:

if ((driver
      .findElement(By.id("da1c"))
      .findElement(By.cssSelector("span"))
      .getAttribute("style")
    ).value > 700) {
       System.out.println("value exceeding")
}

Upvotes: 13

Views: 70213

Answers (3)

Anirudh
Anirudh

Reputation: 2326

You may capture the Computed Css value as shown in the firebug screenshot below:

enter image description here

like this:

WebDriver web = new FirefoxDriver(;
String visibility = web.findElement(By.xpath("//your xpath")).getCssValue("display");

Upvotes: 22

driver.findElement(By.locator( yourLocator )).getAttribute( requiredAttribute )

It will return String

Upvotes: 0

ddavison
ddavison

Reputation: 29032

If you execute .getAttribute("style") on that span, you will recieve a String.

left: 666px; top: 27px;
You can use string manipulation to fetch a particular style.

Alternatively, you can execute some javascript magic using the JavaScriptExecutor, and fetching the left value directly by

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

and then check it from there.

Upvotes: 11

Related Questions