HtmlUnitDriver to invoke local html file

238 Views Asked by At

I am trying to invoke a locally stored html file using the get() method of the HtmlUnitDriver, but the page is not getting loaded. I tried to get the page source and I got 404 not found as the response. If I use the chrome driver in headless mode I am able to invoke the file. Is it possible to do this using HtmlUnitDriver?

Below is a code snippet

HtmlUnitDriver unitDriver = new HtmlUnitDriver();

public class Test {

  public void clickOn() {
    String filePath =
            "home/test-output/100071234.html";
    try {
      unitDriver.get(filePath);
      WebElement ele = unitDriver.findElement(By.id("submitPayment"));
      ele.click();
    } catch (Exception ex) {
      ex.printStackTrace();
  }
}
1

There are 1 best solutions below

1
Alexey R. On

If you need to open local file you have to adhere the URI format. So for Linux it would be (if home folder is under the root folder):

String filePath =
            "file:///home/test-output/100071234.html";

Here you can find other examples (Windows, Mac OS): https://en.wikipedia.org/wiki/File_URI_scheme