selenium webdriver學習---實現簡單的翻頁,將頁面內容的標題和標題鏈接取出;
該情況適合能能循環page=1~n,並且每個網頁隨着循環可以打開的情況,
注意一定是自己拼接的url可以打開,如:http://ask.testfan.cn/articles?page=15,就可以翻到文章分類的第15頁;
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import org.omg.CORBA.PUBLIC_MEMBER; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.Wait; import org.openqa.selenium.support.ui.WebDriverWait; public class YsfTest_20180727{ private static final int ExpectedCondition = 0; private static final int Boolean = 0; public static void main(String[] args) throws InterruptedException, IOException{ WebElement search = null; System.setProperty("webdriver.chrome.driver","C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe"); WebDriver driver = new ChromeDriver(); int pageNum = 15; int i =1; while(i <= pageNum){ driver.get("http://ask.testfan.cn/articles?page="+ i); //窗口最大化 driver.manage().window().maximize(); //將title里面的a標簽取出 List<WebElement> ll = driver.findElements(By.cssSelector(".title > a")); //循環a標簽 for(WebElement w:ll){ //將a標簽對應的文本取出 System.out.println(w.getText()); //將a標簽下href的元素值url取出 System.out.println(w.getAttribute("href")); } System.out.println("第"+i+"頁面抓取完畢"); i = i + 1; } System.out.println("全部抓取完畢"); driver.close(); } }
該示例抓取的是Testfan軟件測試社區的文章標題及鏈接(只抓了15頁),抓取結果以第一頁為例:
****************
【工具分享】Jmeter大文件分析利器,比官方快30倍的分析工具
http://ask.testfan.cn/article/1275
Selenium之操作360瀏覽器
http://ask.testfan.cn/article/1223
Testfan3月接口免費福利課程——秒殺說明
http://ask.testfan.cn/article/1201
Python覆蓋率
http://ask.testfan.cn/article/1193
2018職業測試必讀書單
http://ask.testfan.cn/article/1191
Selenium——去掉Chrome正受到自動軟件測試的控制(Java)
http://ask.testfan.cn/article/1187
【原創】appium-desktop版本配置命令行運行服務(Mac)
http://ask.testfan.cn/article/1186
【原創】appium-desktop版本配置命令行運行服務(windows)
http://ask.testfan.cn/article/1185
Macaca環境配置及樣例執行
http://ask.testfan.cn/article/1181
Selenium環境匯總
http://ask.testfan.cn/article/1173
Appium Hybrid混合應用測試——Native切換WebView
http://ask.testfan.cn/article/1169
【Android 】查看被測應用程序package和launchable-activity
http://ask.testfan.cn/article/1168
快捷定位Appium滑動坐標
http://ask.testfan.cn/article/1158
測試用例的設計方法
http://ask.testfan.cn/article/1157
測試工作常用命令
http://ask.testfan.cn/article/1153
jekins安裝文檔
http://ask.testfan.cn/article/1152
Qtp常見問題解答(百度整理)
http://ask.testfan.cn/article/1151
Testfan10月戶外爬山活動報名中
http://ask.testfan.cn/article/1150
APP測試基本流程
http://ask.testfan.cn/article/1149
軟件測試面試題:軟件測試工具的應用
http://ask.testfan.cn/article/1148
第1頁面抓取完畢
******************
本例用到,窗口最大化:driver.manage().window().maximize();
將title里面的a標簽取出並放在list里:
List<WebElement> ll = driver.findElements(By.cssSelector(".title > a"));
將a標簽對應的文本取出:w.getText();
將a標簽下href的元素值url取出:w.getAttribute("href");