簡體   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