模拟浏览器自动化测试工具Selenium之三页面窗口切换开发篇
生活随笔
收集整理的這篇文章主要介紹了
模拟浏览器自动化测试工具Selenium之三页面窗口切换开发篇
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
不同網站有不同特性,需要掌握selenim基本網頁元素定位以及瀏覽器控制,本文解析網頁主要實現以下操作:
1)模擬鼠標操作,點擊按鈕提交;2)獲取瀏覽器窗口句柄,切換到當前窗口下操作;3)處理不帶總頁數的列表頁,設計兩個變量iDyn和iSta并定位下一頁來翻譯;4)處理彈出框alert;5)通過對元素定位支持不同網頁模板的信息采集,元素下再采集子元素;6)網頁內table元素的處理。具體代碼如下:
package com.test;import java.util.List; import java.util.Set; import java.util.regex.Pattern;import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.NoAlertPresentException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait;import com.util.Logs;public class Shop114YP {public static void main(String[] args) {System.getProperties().setProperty("webdriver.chrome.driver","D:\\dev\\workspace\\ocweb\\libs\\chromedriver.exe"); WebDriver webDriver = new ChromeDriver();WebDriver wd = new ChromeDriver();int iCount=0;//統計輸出多少try {webDriver.get("url");//訪問網址//等待頁面加載完畢,直到條件滿足 (new WebDriverWait(webDriver, 30)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver dr) { int index = dr.getPageSource().indexOf("keyword"); if(index != -1){ return true; //找到,退出等待}else{ return false; //未找到,繼續等待} } });//通過 id 找到 input的 DOMActions action=new Actions(webDriver); //模擬鼠標操作WebElement eleKeyword = webDriver.findElement(By.name("keyword"));action.sendKeys(eleKeyword,"內容").perform();//獲取窗口當前句柄Set<String> bHandlers = webDriver.getWindowHandles();String bHandler = bHandlers.iterator().next();//執行點擊操作,打開一個新的頁面WebElement eleSearch = webDriver.findElement(By.className("search_shu4")); action.moveToElement(eleSearch); action.click().perform(); //打開一個新頁面//獲取窗口當前句柄Set<String> nHandlers = webDriver.getWindowHandles();nHandlers.remove(bHandler);//移除先前頁面String nHandler = nHandlers.iterator().next();//獲取最新打開的句柄webDriver.switchTo().window(nHandler);//切換到新打開的頁面//webDriver.switchTo().window(bHandler);//返回原頁面(new WebDriverWait(webDriver, 30)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver dr) { int index = dr.getPageSource().indexOf("內容"); if(index != -1) return true; //找到,退出等待else return false; //未找到,繼續等待 } });//進入搜索結果頁面,第一頁int iDyn=1;int iSta=1; while(iSta==iDyn){List<WebElement> comList =webDriver.findElements(By.partialLinkText("內容"));for(WebElement ele:comList){//處理每一項iCount++;String com=ele.getText();//公司名String link=ele.getAttribute("href"); //if(!link.contains("shopid")) continue;//不包含shopid的鏈接不要wd.get(link); try{Alert alert = wd.switchTo().alert();//判斷是否有js彈出框if (null == alert){//沒彈出throw new NoAlertPresentException();}else {alert.accept();continue;}}catch (NoAlertPresentException e) {//System.out.println("There is no alert appear!");} //支持不同網頁模板的信息采集if(wd.getPageSource().contains("lianxi-text")){//打開頁面List<WebElement> contactList =wd.findElements(By.id("lianxi-text"));for(WebElement eleCon:contactList){String txt=eleCon.getText();com=com+"|"+txt;} }else if(wd.getPageSource().contains("tablebg")){//網頁table處理WebElement table = wd.findElement(By.className("tablebg")); //獲取table元素List<WebElement> rows = table.findElements(By.tagName("tr"));//獲取table內行元素//List<WebElement> cells=rows.get(0).findElements(By.tagName("td"));//獲取row第一行中的所有列for(WebElement row:rows) com=com+"|"+row.getText();}else if(wd.getPageSource().contains("table2")){List<WebElement> cells = wd.findElements(By.tagName("td"));//獲取td元素for(WebElement cell:cells){if(cell.getAttribute("class").equals("table2") || cell.getAttribute("class").equals("table") ){com=com+"|"+cell.getText();}}}else if(wd.getPageSource().contains("sidenei")){WebElement elesn=wd.findElement(By.className("sidenei"));List<WebElement> eleNeis = elesn.findElements(By.className("nei"));//class等于nei的元素for(WebElement elenei:eleNeis) com=com+"|"+elenei.getText();}else if(wd.getPageSource().contains("liebiao")){WebElement eleLibiao=wd.findElement(By.className("liebiao"));List<WebElement> eleLbs = eleLibiao.findElements(By.id("li"));//ul下li元素for(WebElement eleLb:eleLbs) com=com+"|"+eleLb.getText();}Logs.writeLogs(com); }//進入下一頁,找到下一頁鏈接iDyn++;WebElement pageEle =webDriver.findElement(By.linkText("下一頁"));String pagelink=pageEle.getAttribute("href");Pattern ps= Pattern.compile("[?&=]+");String[] values=ps.split(pagelink);iSta=Integer.valueOf(values[values.length-1]);webDriver.get(pagelink);(new WebDriverWait(webDriver, 30)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver dr) { int index = dr.getPageSource().indexOf("內容"); if(index != -1) return true; //找到,退出等待else return false; //未找到,繼續等待 } }); } }catch (Exception e) { System.err.println( "Exception: " + e );}System.out.println(iCount);wd.close();wd.quit();webDriver.close();webDriver.quit();} }總結
以上是生活随笔為你收集整理的模拟浏览器自动化测试工具Selenium之三页面窗口切换开发篇的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法导论之线性规划
- 下一篇: Java实现算法导论中线性规划单纯形算法