[英]How set relative path in driver.get() method in selenium?
In Selenium I can navigate to a URL using driver.get(DesireURL) method. 在Selenium中,我可以使用driver.get(DesireURL)方法导航到URL。 If I have to navigate to a local HTML file I can use 如果我必须导航到本地HTML文件,则可以使用
driver.get("C://what ever the location of my file");
But if my HTML file located to in my project resource directory like resources/HTML/file.html. 但是,如果我的HTML文件位于我的项目资源目录中,例如resources / HTML / file.html。
I am using this code in Ubuntu and windows so a don't use absolute path. 我在Ubuntu和Windows中使用此代码,因此请勿使用绝对路径。
How can I browse that file like driver.get(“resources/HTML/file.html”)? 如何浏览类似driver.get(“ resources / HTML / file.html”)的文件?
尝试这个:
driver.get("file:///C://what ever the location of my file");
If your file at any location you can use like this, simply use file:///C:/Sankalp/test.html
address in get("")
method 如果您的文件在任何位置都可以像这样使用,只需在get("")
方法中使用file:///C:/Sankalp/test.html
地址
Try sample code: 尝试示例代码:
public class Hello {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:/Users/sankalp.gupta/Desktop/JAVASEL/geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("file:///C:/Sankalp/test.html"); //replace it with your path
System.out.println(driver.getCurrentUrl());
}
}
Firstly create a AppConstant class where all the constant resources path are specified. 首先创建一个AppConstant类,其中指定了所有常量资源路径。
/*
* used to get the path of necessary resources
*/
public class AppConstant {
// prevents instantiation
private AppConstant () {
}
public static final String CURRENT_DIR = System.getProperty("user.dir");
public static final String APP_RESOURCE = CURRENT_DIR+"/resources/";
}
Then use the resource path as follows 然后使用资源路径,如下所示
driver.get("file:"+AppConstant.APP_RESOURCE+"HTML/file.html");
EDIT: You can also verify by using this if you do not want to create another class 编辑:如果您不想创建另一个类,也可以使用此方法进行验证
String htmlLocation = "file:"+System.getProperty("user.dir")+"/resources/HTML/Table.html";
driver.get(htmlLocation);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.