Collections:
Load HTML Files with WebDriver in Java
How to load an HTML document file from the local hard disk with WebDriver in Java?
✍: FYIcenter.com
If you want to load an HTML document file from the local hard disk with WebDriver, you can use the "file" URI format to identify the local file as shown in the example program below:
// LoadLocalHtmlFile.java // Copyright (c) FYIcenter.com import org.openqa.selenium.*; import java.nio.file.*; public class LoadLocalHtmlFile { public static void main(String[] args) { WebDriver driver = WebDriverLoader.load(args[0]); JavascriptExecutor executor = (JavascriptExecutor) driver; String url = Paths.get(args[1]).toUri().toString(); System.out.println(url); driver.get(url); 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()+">"); } } }
To test the above program, you can use the following HTML file, Hello.html:
<html> <head> <title>Hello</title> </head> <body bgcolor="#ffeeee"> <p>Hello world!</p> </body> </html>
Compile and run the program on Hello.html:
C:\fyicenter> javac -classpath \ .;\fyicenter\selenium\java\client-combined-3.141.59.jar \ LoadLocalHtmlFile.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 \ LoadLocalHtmlFile Chrome Hello.html file:///C:/fyicenter/Hello.html <html{}> <head{}> <title{}> </title> </head> <body{bgcolor=#ffeeee}> <p{}> </p> </body> </html>
The output shows that we loaded the local HTML file correctly.
⇒ getCurrentUrl() Failed on WebDriver Java API for Edge
⇐ Dump Element Attributes with JavascriptExecutor in Java
2019-12-19, 2709🔥, 0💬
Popular Posts:
How to call JMeter command in a Windows batch file? I want to create a single batch job to run JMete...
How to generate date and time test values? Timestamp values are frequently needed in testing almost ...
How to generate email addresses? To help you to obtain some email addresses for testing purpose, FYI...
How to generate ISBN numbers? To help you to obtain some ISBN numbers for testing purpose, FYIcenter...
How to convert a date and time to a standard format? Date and time can be displayed in a number of s...