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, ∼4110🔥, 0💬
Popular Posts:
How to access Response Status Code 400 as Success? My sampler failed, because the server returns the...
How to generate test credit card numbers? Test credit card numbers are frequently needed in testing ...
How to validate Mod 10 (Luhn Algorithm) checksum? In order to help your programming or testing tasks...
Where to find tutorials on Test Management? I want to know how to manage software testing documents ...
What is this Website about? Welcome to the Software Quality Assurance (QA) and Testing Information C...