简体   繁体   English

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

[英]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.

相关问题 如何使用java在selenium中显式地等待driver.get() - how to give wait to driver.get() explicitly in selenium using java 如何在 Selenium 中使用带有字符串枚举的 driver.get() - How to use driver.get() with string enum in Selenium Java Selenium卡在“ driver.get”上 - Java Selenium stuck on 'driver.get' 设置 selenium web 驱动程序的相对路径 - Set relative path for selenium web driver 在Appium driver.get方法中不支持&符 - Ampersand is not supporting in Appium driver.get method 一旦调用了 driver.get(URL) 方法,Selenium Java 客户端就无法取回控制权 - Selenium Java Client is not getting back the control once driver.get(URL) method is invoked Selenium Web Driver似乎间歇性地跳过driver.get() - Selenium Web Driver seems to skip driver.get() intermittently java.lang.IllegalStateException:驱动程序可执行文件的路径必须由webdriver.chrome.driver系统属性设置; (驱动程序.get) - java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; (driver.get) 如何在Selenium中创建一个driver.get函数,该函数将从另一个类调用? - How do I create a driver.get function in Selenium that will be called from another class? Java Selenium - 绕过 driver.get() 等待时间 - Java Selenium - Bypass driver.get() waiting time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM