繁体   English   中英

如何在硒的driver.get()方法中设置相对路径?

[英]How set relative path in driver.get() method in selenium?

在Selenium中,我可以使用driver.get(DesireURL)方法导航到URL。 如果我必须导航到本地HTML文件,则可以使用

driver.get("C://what ever the location of my file");

但是,如果我的HTML文件位于我的项目资源目录中,例如resources / HTML / file.html。

我在Ubuntu和Windows中使用此代码,因此请勿使用绝对路径。
如何浏览类似driver.get(“ resources / HTML / file.html”)的文件?

尝试这个:

driver.get("file:///C://what ever the location of my file");

如果您的文件在任何位置都可以像这样使用,只需在get("")方法中使用file:///C:/Sankalp/test.html地址

尝试示例代码:

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());
}

}

首先创建一个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/";  
 }

然后使用资源路径,如下所示

 driver.get("file:"+AppConstant.APP_RESOURCE+"HTML/file.html");

编辑:如果您不想创建另一个类,也可以使用此方法进行验证

String htmlLocation = "file:"+System.getProperty("user.dir")+"/resources/HTML/Table.html";
driver.get(htmlLocation);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM