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

大兔子大兔子 提交于 2019-12-17 06:46:30

问题


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").


回答1:


Try using this method:

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

(or)

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



回答2:


This can also be done with a relative file:

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



回答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).




回答4:


For those of us using java.nio, we can also do the following:

webdriver.get("file:\\\\\\" + filePath);

...where filePath is an object of type java.nio.file.Path and represents an absolute path.



来源:https://stackoverflow.com/questions/17972885/how-to-use-selenium-webdriver-on-local-webpage-on-my-pc-instead-of-one-located

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!