日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

模拟浏览器自动化测试工具Selenium之二Html基本元素开发篇

發布時間:2025/4/16 HTML 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模拟浏览器自动化测试工具Selenium之二Html基本元素开发篇 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

發現用IE瀏覽器,有很多動態網站加載錯誤,只好安裝chrome瀏覽器,然后下載chrome driver來驅動。通過selenium的基本元素定位操作來和網頁交互。

網頁解析主要動作:1)表單自動填寫和提交;2)處理帶有總頁數的翻頁及每頁列表;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; //未找到,繼續等待} } });//通過 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; //未找到,繼續等待 } });//進入搜索結果頁面,先獲取總頁數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; //未找到,繼續等待 } });//定位公司名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);// 創建 Pattern 對象 Matcher m = r.matcher(line.trim());// 創建 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);// 創建 Pattern 對象 Matcher m = r.matcher(line.trim());// 創建 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; //未找到,繼續等待 } });} // 關閉窗口,釋放資源。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基本元素开发篇的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。