使用 Selenium WebDriver 獲取當前頁面 URL。


我們可以使用 Selenium Webdriver 獲取當前頁面的 URL。這是在 getCurrentUrl() 方法的幫助下實現的。它獲取開啟的應用程式的 URL。此方法不接受任何引數,並以字串的形式獲取 URL。

語法 -

String strUrl = driver.getCurrentUrl();

示例

程式碼實現。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class CurrentUrl{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://tutorialspoint.tw/index.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // get current URL and print
      String strUrl = driver.getCurrentUrl();
      System.out.println("Current Url is:"+ strUrl);
      driver.quit();
   }
}

結果

更新於: 2020 年 8 月 28 日

18K+ 瀏覽

開啟你的 職業

完成課程獲得認證

開始學習
廣告