(java)selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出

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

 

posted @ 2018-07-28 11:30  夏日的向日葵  阅读(2980)  评论(0编辑  收藏  举报