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, ∼4192🔥, 0💬
Popular Posts:
How to generate test fractional numbers? Test fractional numbers are numbers with fractions that are...
How to use HTTP protocol to post an XML message to a server? You can follow this tutorial to post an...
How to update hidden input value field value with WebDriver in Python? Normally, a hidden input valu...
How to generate test fractional numbers? Test fractional numbers are numbers with fractions that are...
How to generate IP Addresses? To help you to obtain some IP addresses for testing purpose, FYIcenter...