How to use Selenium WebDriver on local webpage (on my PC) instead of one located somewhere online?

41,114

Solution 1

Try using this method:

webdriver.get("file:///D:/folder/abcd.html");

(or)

selenium = new WebDriverBackedSelenium(driver, "file:///D:/folder/abcd.html");

Solution 2

This can also be done with a relative file:

Path sampleFile = Paths.get("sample.html");
driver.get(sampleFile.toUri().toString());

Solution 3

When you call the driver.get(URL) method, WebDriver looks for HTTP request using as base javascript, Therefore, refering to a website as a path, that task won't be possible.

But it will be possible if you : 1st- Install Apache WebServer (let's say) on your marchine. 2nd- Upload or expose to the WebServer, that web application (dispatcher.html) 3rd- Try recording and executing your testcases on [http://localhost:8080/dispatcher.html] (8080 is the default port but you can configure it to other).

Solution 4

You can always drag and drop html file from your PC on open web browser during selenium session and see how file path looks. In my case it is:

webdriver.get("file:///C:/Users/Desktop/Some%20%E2%80%93%20file%20on%20the%20PC.html")

Solution 5

Selenium Version: 3.141.59

Use this webdriver.get("file:///D:/folder/abcd.html") get failed.

Instead of webdriver.get("///D:/folder/abcd.html") get successfully.

Share:
41,114
Bartosz Wyględacz
Author by

Bartosz Wyględacz

Updated on May 01, 2020

Comments

  • Bartosz Wyględacz
    Bartosz Wyględacz about 4 years

    I want to use Selenium WebDriver on a webpage that I have on my hard disc. I've tried to something like:

    selenium = new WebDriverBackedSelenium(driver, "C:\\...dispatcher.html");
    

    ...instead of the normal:

    selenium = new WebDriverBackedSelenium(driver, "http://www.dunnowhattodo.org");
    

    ...but it doesn't work (I get the error "unknown protocol: c").

  • Pithikos
    Pithikos over 9 years
    Can relative paths be used somehow?
  • Simon Baars
    Simon Baars almost 7 years
    @Pithikos no, but when using windows you can use the environment variables, like %USERPROFILE% (home folder) and %APPDATA% (application folder)
  • TestAutomator
    TestAutomator almost 7 years
    How do you do this on macOS with Safari? I can't open html files from the local file system. It's working if I use file:///Users/pathToMyFile/index.html manually in the browser. However it's not working if the driver opens it by the get method.
  • DANIEL ROSAS PEREZ
    DANIEL ROSAS PEREZ over 2 years
    It worked on my aws instance :)