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学习---实现简单的翻页,将页面内容的标题和标题链接取出的更多相关文章

  1. (java)selenium webdriver学习--通过id、name定位,输入内容,搜索,关闭操作、通过tagname查找元素

    selenium webdriver学习--通过id.name定位,输入内容,搜索,关闭操作:通过tagname查找元素 打开谷歌浏览器,输入不同的网站,搜索框的定位含有不同元素(有时为id,有时为n ...

  2. (java)selenium webdriver学习,选择模块,点击下一页,获取当前url

    selenium webdriver学习,选择模块,点击下一页,获取当前url 查找下一页有多种方法,这里列举两种: isSelected()函数用于判断是否点击选中,返回Boolean类型 impo ...

  3. selenium webdriver学习(二)————对浏览器的简单操作(转载JARVI)

    selenium webdriver学习(二)————对浏览器的简单操作 博客分类: Selenium-webdriver   selenium webdriver对浏览器的简单操作 打开一个测试浏览 ...

  4. (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待

    selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待 本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作: import java. ...

  5. (java)selenium webdriver学习--打开新窗口,并判断新窗口是否与目标窗口一致

    描述:selenium webdriver学习--打开新窗口,并判断新窗口是否与目标窗口一致,若一致则切换到该窗口并获取标题 跳出if判断,获取父级标题,并关闭 HTML标签不太明显时,可以用路径表示 ...

  6. selenium webdriver学习(六)------------如何得到弹出窗口

    selenium webdriver学习(六)------------如何得到弹出窗口 在selenium 1.X里面得到弹出窗口是一件比较麻烦的事,特别是新开窗口没有id.name的时候.当时还整理 ...

  7. selenium webdriver学习(四)------------定位页面元素(转)

    selenium webdriver学习(四)------------定位页面元素 博客分类: Selenium-webdriver seleniumwebdriver定位页面元素findElemen ...

  8. selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面(转)

    selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面 博客分类: Selenium-webdriver 元素拖放drag and drop  Q群里 ...

  9. selenium webdriver学习(九)------------如何操作cookies(转)

    selenium webdriver学习(九)------------如何操作cookies 博客分类: Selenium-webdriver   Web 测试中我们经常会接触到Cookies,一个C ...

随机推荐

  1. tween算法

    tween算法 https://www.cnblogs.com/cloudgamer/archive/2009/01/06/Tween.html 参数说明: t: current time:当前时间: ...

  2. LeetCode 704. 二分查找(Binary Search)

    704. 二分查找 704. Binary Search 题目描述 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果 ...

  3. Spring MVC原理图及其重要组件

    一.Spring MVC原理图: ps: springmvc的运行流程为图中数字序号 二.springmvc的重要组件: 1)前端控制器:DispatchServlet(不需要程序员开发) 接收请求, ...

  4. nginx acces.log日志分析

    1,统计各访问IP的总数 awk '{if($9>0 && $9==200 && substr($6,2)== "GET") a[$1]++} ...

  5. 基于卷积神经网络的人脸识别项目_使用Tensorflow-gpu+dilib+sklearn

    https://www.cnblogs.com/31415926535x/p/11001669.html 基于卷积神经网络的人脸识别项目_使用Tensorflow-gpu+dilib+sklearn ...

  6. Python基础学习:字符串认知与应用

    一.len()  家电维修 len:全写是length,是计算容量的函数:例如a="1234",len(a)=4 如果是中文字符,比如a="哈",len(a)= ...

  7. 跨域和CORS

    一 跨域 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之上的 ...

  8. epoll_ctl函数的使用

    #include <sys/epoll.h> int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);作用: ...

  9. Python爬虫b站视频弹幕并生成词云图分析

    爬虫:requests,beautifulsoup 词云:wordcloud,jieba 代码加注释: # -*- coding: utf-8 -*- import xlrd#读取excel impo ...

  10. proxy_banner