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, ∼3764🔥, 0💬
Popular Posts:
How to Pass Windows Environment Variable to JMeter? I want to use Windows temporary folder environme...
How to see my IP address Host Name? To help you to see your IP Address Host Name, FYIcenter.com has ...
How to Open and Close Internet Explorer with UFT script? One way to open and close Internet Explorer...
How to convert IPv4 to IPv6 addresses? In order to help your programming or testing tasks, FYIcenter...
What are JMeter command line options? You can get see JMeter command line options by running this co...