Getting values ​​of all CSS properties of a selected element in Selenium

Suppose I found an element by its XPath using:

WebElement we = driver.findElement(By.xpath("some XPath"));

I know that I can get the value of a specific CSS property using we.getCssValue("some property"), but can I get the values ​​of all properties without explicitly specifying their names?

+4
source share
1 answer

Unfortunately,

this is not possible using the native Selenium API.

But with Javascript you can:

You can use some javascript support using Seleniums functionality JavascriptExecutor.executeScript.

js ( @Mahsum Akbas)

Java/Selenium, "css-attribute01: value01; css-attribute02: value02;".

, css- .

WebElement we = driver.findElement(By.tagName("div"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
String script = "var s = '';" +
                "var o = getComputedStyle(arguments[0]);" +
                "for(var i = 0; i < o.length; i++){" +
                "s+=o[i] + ':' + o.getPropertyValue(o[i])+';';}" + 
                "return s;";

System.out.println(executor.executeScript(script, we));

script . , , . .

Update

, "" Selenium, @JeffC:

driver.findElement(By.tagName("div")).getAttribute("style")

:

" " CSS-, . , .

+11

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


All Articles