模拟浏览器自动化测试工具Selenium之二Html基本元素开发篇
生活随笔
收集整理的這篇文章主要介紹了
模拟浏览器自动化测试工具Selenium之二Html基本元素开发篇
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
發(fā)現(xiàn)用IE瀏覽器,有很多動態(tài)網站加載錯誤,只好安裝chrome瀏覽器,然后下載chrome driver來驅動。通過selenium的基本元素定位操作來和網頁交互。
網頁解析主要動作:1)表單自動填寫和提交;2)處理帶有總頁數(shù)的翻頁及每頁列表;3)對網頁具體元素內的問題進行正則匹配采集信息。
具體代碼參考如下:
package com.test;import java.io.BufferedReader; import java.io.StringReader; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern;import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait;import com.util.Logs;public class BrasShopYP {public static void main(String[] args) {try {System.getProperties().setProperty("webdriver.chrome.driver","D:\\tmp\\chromedriver.exe");WebDriver webDriver = new ChromeDriver();//訪問網址webDriver.get("URL");//等待頁面加載完畢,直到條件滿足 (new WebDriverWait(webDriver, 30)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver dr) { int index = dr.getPageSource().indexOf("id"); if(index != -1){ return true; //找到,退出等待}else{ return false; //未找到,繼續(xù)等待} } });//通過 id 找到 input的 DOMWebElement element = webDriver.findElement(By.id("name"));// 輸入關鍵字element.sendKeys("內容");// 提交 input所在的 formelement.submit(); //等待頁面加載完畢(new WebDriverWait(webDriver, 30)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver dr) { int index = dr.getPageSource().indexOf("name"); if(index != -1) return true; //找到,退出等待else return false; //未找到,繼續(xù)等待 } });//進入搜索結果頁面,先獲取總頁數(shù)WebElement eleDivTotal =webDriver.findElement(By.cssSelector("div.total.ng-binding"));int iTotal=Integer.valueOf(eleDivTotal.getText().substring(1, 3));for(int i=1;i<=iTotal;i++){//處理每一頁List<WebElement> comList =webDriver.findElements(By.partialLinkText("內容"));for(WebElement ele:comList){//處理每一項String link=ele.getAttribute("href"); WebDriver wd = new ChromeDriver();wd.get(link);//等待頁面加載完畢(new WebDriverWait(wd, 50)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver dr) { int index = dr.getPageSource().indexOf("name"); if(index != -1) return true; //找到,退出等待else return false; //未找到,繼續(xù)等待 } });//定位公司名WebElement elecom =wd.findElement(By.tagName("p"));//定位電話和郵箱BufferedReader reader = new BufferedReader(new StringReader(wd.getPageSource()));String strTel="null"; while ( reader.readLine()!= null) {//定位電話String line = reader.readLine();if(line !=null){String regex="^1[3|4|5|8][0-9]\\d{8}$";//手機正則表達式Pattern r = Pattern.compile(regex);// 創(chuàng)建 Pattern 對象 Matcher m = r.matcher(line.trim());// 創(chuàng)建 matcher 對象 if(m.find()) {//滿足正則表達式strTel=line.trim();break;}}}//定位郵箱String strEmail="null";while ( reader.readLine()!= null) {//定位郵箱String line = reader.readLine();if(line !=null){String regex="^[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}$";//email正則表達式Pattern r = Pattern.compile(regex);// 創(chuàng)建 Pattern 對象 Matcher m = r.matcher(line.trim());// 創(chuàng)建 matcher 對象 if(m.find()) {//滿足正則表達式strEmail=line.trim();break;}}} //輸出公司|電話|郵箱Logs.writeLogs(elecom.getText()+"|"+strTel+"|"+strEmail);wd.close();//關閉瀏覽器窗口wd.quit();//關閉chrome Driver進程;}//處理下一頁if(i==iTotal) break;WebElement elePage = webDriver.findElement(By.linkText(String.valueOf(i+1)));webDriver.navigate().to(elePage.getAttribute("href"));//等待頁面加載完畢(new WebDriverWait(webDriver, 30)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver dr) { int index = dr.getPageSource().indexOf("b-c-white search_result_container"); if(index != -1) return true; //找到,退出等待else return false; //未找到,繼續(xù)等待 } });} // 關閉窗口,釋放資源。webDriver.close();//關閉瀏覽器窗口webDriver.quit();//關閉chrome Driver進程;}catch (Exception e) {System.err.println( "Exception: " + e ); }} }//參考:http://blog.csdn.net/wx19900503/article/details/47169107 //參考:http://www.cnblogs.com/TankXiao/p/5222238.html總結
以上是生活随笔為你收集整理的模拟浏览器自动化测试工具Selenium之二Html基本元素开发篇的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 模拟浏览器自动化测试工具Selenium
- 下一篇: 算法导论之线性规划