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

I want to check if the value of a style element is greater than a certain value (i.e.> 666px left?), But I cannot get the value.

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

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

I use this code to try to print its value, but it does not print:

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

I need something like this:

 if ((driver.findElement(By.id("da1c")).findElement(By.cssSelector("span")).getAttribute("style")).value> 700) { System.out.println("value exceeding") } 
+6
source share
3 answers

You can 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"); 
+9
source

If you execute .getAttribute("style") on this gap, you will get a string.

left: 666px; top: 27px;
You can use string manipulations to get a specific style.

Alternatively, you can do some javascript magic with JavaScriptExecutor and get left value directly with

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

and then check it there.

+7
source
 driver.findElement(By.locator( yourLocator )).getAttribute( requiredAttribute ) 

It will return a string

0
source

Source: https://habr.com/ru/post/952539/


All Articles