(java)selenium webdriver學習---實現簡單的翻頁,將頁面內容的標題和標題連結取出

來源:互聯網
上載者:User

標籤:查看   getattr   檔案   題解   man   class   最大   執行   expected   

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");

 

(java)selenium webdriver學習---實現簡單的翻頁,將頁面內容的標題和標題連結取出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.