Selenium為訪問站點提供了兩種方式:
driver.get("URL"); #個人推薦這種方式,因為能少寫一個字符是一個字符啊。
driver.navigate().to("URL");
頁面的Title主要用于case的驗證,當然了也有一位大神教育我說case不夠title來湊,這個作用顯著啊!
頁面的URL也主要是用于case的驗證,URL的一個重要的測試領域是對于重定向的測試(很多地址當你訪問后會自動的跳轉到其他的地址,這時URL 驗證的機會來了,當然了還是有很多其他的地方能用到一時想不出來了這些吧!)
package org.coderinfo.demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GetWebSiteAndPrintWebInfo {
private static final String URL = "http://www.google.com.hk";
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize(); //大化瀏覽器界面
driver.get(URL); // 等同于 driver.navigate().to(URL); 訪問谷哥的首頁 ,此處放棄度娘。
String title = driver.getTitle(); //獲取當前頁面的title
String currentUrl = driver.getCurrentUrl(); // 獲取當前頁面的URL
System.out.printf("Current page's title is: %s , Current URL is: %s
",title,currentUrl);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.quit(); //徹底退出WebDriver
}
}