1. Selenium的配置
1.1. 在項目中引入Selenium庫
通過Maven加入。
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
1.2. 下載chromedriver.exe
1.3. 在項目代碼中加入chromedriver位置的配置
System.getProperties().setProperty("webdriver.chrome.driver", "chromedriver.exe");
2. 使用Selenium
2.1. Selenim語法
Selenium的語法和一些相關資料可以看這個博客,需要用哪里直接去查閱即可。
2.2. 智能等待
首先閱讀這篇java selenium (十三) 智能等待頁面加載完成
由於Java8的lambda表達式可以用於顯示等待,以及我個人模擬登陸過程中的一些心得,我這里補充一點內容。sleep()方法和pageLoad方法我就不再介紹,因為不推薦使用。
2.2.1. 隱式等待
最常用的等待方式,也比較簡單,是全局的。
webDriver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
在設定的時間內Selenium會嘗試獲取所要求的元素,很簡單,對於簡單的查找元素等已經足夠。但是如果需要監測一些元素的狀態,那還需要調用顯示等待的方法。
2.2.2. 顯式等待
顯示等待方法需要構造一個WebDriverWait對象,其接收webDriver和設定時長(單位秒)作為構造器參數構造。
當需要等待時,則調用它的until方法,until方法接收一個ExceptedCondition<>類對象,這是一個函數對象,因此,我們可以用Java8的lambda表達式去構建。
這里舉例:
webDriverWait.until((ExpectedCondition<WebElement>) w -> w.findElement(By.xpath("//div[@class='home-sub-nav layout-box']/a[4]"))).click();
webDriverWait.until((ExpectedCondition<WebElement>) w -> w.findElement(By.xpath("//div[@class='card card2 line-around']/div[1]/a[2]"))).click();
不過,通常情況下,ExceptedConditions類已經定義了我們需要的動作,而不需要自己再去實現這個接口,比如上面例子中的方法就可以用presenceOfElementLocated(By locator)
方法去檢查,當然,通常依賴隱式等待就可以了。
這里舉一個必須用顯示等待的例子,比如微博的登錄界面,其輸入框在剛開始時就存在,但不可用,我們需要檢測到,當它可用時再去運行,這里就需要使用顯示等待的方法:
WebDriverWait webDriverWait=new WebDriverWait(webDriver,10);
webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("loginName"))).sendKeys(args[0]);
webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("loginPassword"))).sendKeys(args[1]);
此處調用了elementToBeClickable方法,參考文章中有列出ExpectedConditions類中包含的方法,我在這里列出一下:
等待的條件 | ExpectedConditions方法 |
---|---|
頁面元素是否在頁面上可用和可被單擊 | elementToBeClickable(By locator) |
頁面元素處於被選中狀態 | elementToBeSelected(WebElement element) |
頁面元素在頁面中存在 | presenceOfElementLocated(By locator) |
在頁面元素中是否包含特定的文本 | textToBePresentInElement(By locator) |
頁面元素值 | textToBePresentInElementValue(By locator, java.lang.String text) |
標題 (title) | titleContains(java.lang.String title) |
利用這些方法可以很好的監測到我們要的控件是否可用。
3. 模擬登陸並獲取Cookie的代碼
public class WeiboLoginAndGetCookie {
public static void main(String[] args) throws Exception{
//配置ChromeDiver
System.getProperties().setProperty("webdriver.chrome.driver", "chromedriver.exe");
//開啟新WebDriver進程
WebDriver webDriver = new ChromeDriver();
//全局隱式等待
webDriver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//設定網址
webDriver.get("https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F");
//顯示等待控制對象
WebDriverWait webDriverWait=new WebDriverWait(webDriver,10);
//等待輸入框可用后輸入賬號密碼
webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("loginName"))).sendKeys(args[0]);
webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("loginPassword"))).sendKeys(args[1]);
//點擊登錄
webDriver.findElement(By.id("loginAction")).click();
//等待2秒用於頁面加載,保證Cookie響應全部獲取。
sleep(2000);
//獲取Cookie並打印
Set<Cookie> cookies=webDriver.manage().getCookies();
Iterator iterator=cookies.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next().toString());
}
//關閉WebDriver,否則並不自動關閉
webDriver.close();
}
}