Collections:
Dump Element Attributes with JavascriptExecutor in Java
How to dump all attributes of an HTML element with WebDriver in Java?
✍: FYIcenter.com
The WebElement API provided in WebDriver offers only one method to retrieve the attribute value of a given attribute name using the getAttribute(name) method.
So there is no way to dump all attributes of an HTML element with the WebElement API, since you don't know what attributes are specified in the HTML element.
One way to solve the problem is to run a JavaScript code use the JavascriptExecutor interface, as shown in the following example program.
// DumpElementsWithAttributes.java // Copyright (c) FYIcenter.com import org.openqa.selenium.*; public class DumpElementsWithAttributes { public static void main(String[] args) { WebDriver driver = WebDriverLoader.load(args[0]); JavascriptExecutor executor = (JavascriptExecutor) driver; driver.get("http://sqa.fyicenter.com"); WebElement html = driver.findElement(By.tagName("html")); printTagWithAttributes(html,"",executor); driver.quit(); } public static void printTagWithAttributes(WebElement tag, String space, JavascriptExecutor executor) { String script = "var items = {};" + " for (index = 0; index < arguments[0].attributes.length; ++index)" + " { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value };" + " return items;"; Object attributes = executor.executeScript(script,tag); java.util.List<WebElement> list = tag.findElements(By.xpath("./*")); if (list==null) { System.out.println(space+"<"+tag.getTagName()+attributes+"/>"); } else { System.out.println(space+"<"+tag.getTagName()+attributes+">"); for (WebElement child: list) { printTagWithAttributes(child,space+" ",executor); } System.out.println(space+"</"+tag.getTagName()+">"); } } }
Compile and run the program, you will see that all elements of the Web page are dumped with their attributes in the output:
C:\fyicenter> javac -classpath \ .;\fyicenter\selenium\java\client-combined-3.141.59.jar \ DumpElementsWithAttributes.java C:\fyicenter> java -classpath \ .;\fyicenter\selenium\java\client-combined-3.141.59.jar;\ \fyicenter\selenium\java\libs\guava-25.0-jre.jar;\ \fyicenter\selenium\java\libs\okhttp-3.11.0.jar;\ \fyicenter\selenium\java\libs\okio-1.14.0.jar;\ \fyicenter\selenium\java\libs\commons-exec-1.3.jar \ DumpElementsWithAttributes Chrome <html{}> <head{}> <meta{content=text/html; charset=utf-8, http-equiv=Content-Type}> </meta> <meta{content=IE=edge, http-equiv=X-UA-Compatible}> </meta> ... <link{href=_style.css, rel=stylesheet, type=text/css}> </link> <title{}> </title> <script{}> </script> <script{}> </script> ... </head> <body{}> <div{class=c1, id=left}> <div{class=r2, id=links}> <p{class=head}> </p> <p{class=link}> <a{href=/1000036_Apache_JMeter_Tutorials.html}> </a> </p> <p{class=link}> <a{href=/1000128_Test_Management_Tutorials.html}> </a> </p> ... </div> </div> </body> </html>
The output looks good.
⇒ Load HTML Files with WebDriver in Java
⇐ Dump Elements with By.xpath() in Java
2019-12-19, 1369🔥, 0💬
Popular Posts:
How to force JMeter to automatically flush test result to file immediately after each sampler is exe...
How to validate email address format? In order to help your programming or testing tasks, FYIcenter....
How to generate currency test values? Currency test values are frequently needed in testing date and...
Where to find test data generators? FYIcenter.com has prepared a number of test data generators for ...
Where to find tutorials on Apache JMeter test tool? I want to know how to use Apache JMeter. Here is...