(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");
(java)selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出的更多相关文章
- (java)selenium webdriver学习--通过id、name定位,输入内容,搜索,关闭操作、通过tagname查找元素
selenium webdriver学习--通过id.name定位,输入内容,搜索,关闭操作:通过tagname查找元素 打开谷歌浏览器,输入不同的网站,搜索框的定位含有不同元素(有时为id,有时为n ...
- (java)selenium webdriver学习,选择模块,点击下一页,获取当前url
selenium webdriver学习,选择模块,点击下一页,获取当前url 查找下一页有多种方法,这里列举两种: isSelected()函数用于判断是否点击选中,返回Boolean类型 impo ...
- selenium webdriver学习(二)————对浏览器的简单操作(转载JARVI)
selenium webdriver学习(二)————对浏览器的简单操作 博客分类: Selenium-webdriver selenium webdriver对浏览器的简单操作 打开一个测试浏览 ...
- (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待
selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待 本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作: import java. ...
- (java)selenium webdriver学习--打开新窗口,并判断新窗口是否与目标窗口一致
描述:selenium webdriver学习--打开新窗口,并判断新窗口是否与目标窗口一致,若一致则切换到该窗口并获取标题 跳出if判断,获取父级标题,并关闭 HTML标签不太明显时,可以用路径表示 ...
- selenium webdriver学习(六)------------如何得到弹出窗口
selenium webdriver学习(六)------------如何得到弹出窗口 在selenium 1.X里面得到弹出窗口是一件比较麻烦的事,特别是新开窗口没有id.name的时候.当时还整理 ...
- selenium webdriver学习(四)------------定位页面元素(转)
selenium webdriver学习(四)------------定位页面元素 博客分类: Selenium-webdriver seleniumwebdriver定位页面元素findElemen ...
- selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面(转)
selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面 博客分类: Selenium-webdriver 元素拖放drag and drop Q群里 ...
- selenium webdriver学习(九)------------如何操作cookies(转)
selenium webdriver学习(九)------------如何操作cookies 博客分类: Selenium-webdriver Web 测试中我们经常会接触到Cookies,一个C ...
随机推荐
- java获取当前项目路径System.getProperty("user.dir")
System.getProperty("user.dir") 就是项目的文件夹绝对路径
- 百度地图jsapi 自定义大头针的方法
百度地图jsapi 自定义大头针的方法<pre> var myIcon = new BMap.Icon("http://developer.baidu.com/map/jsdem ...
- LeetCode 227. 基本计算器 II(Basic Calculator II)
227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和 ...
- 13 Spring 的事务控制
1.事务的概念 理解事务之前,先讲一个你日常生活中最常干的事:取钱. 比如你去ATM机取1000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉1000元钱:然后ATM出1000元钱.这两个步骤必 ...
- 11 Reponse对象+ServletContext对象
1.HTTP协议: (1)请求消息:客户端发送给服务器端的数据 数据格式: 1. 请求行 2. 请求头 3. 请求空行 4. 请求体 (2)响应消息:服务器端发送给客户端的数据 * 数据格式: 1. ...
- Spring Boot 项目中的 parent
前言 我们成功创建Spring Boot之后,pom.xml坐标文件中都会有如下一段引用: <parent> <groupId>org.springframework.boot ...
- fwrite & fread 的使用
每一次切换文件操作模式必须调用fclose关闭文件. 如果直接切换操作模式,文件将损坏(出现乱码)或操作失败. 在调用了fclose时,作为参数的文件指针将被回收,必须再次定义,因此最好将功能封装. ...
- Django模板语言中静态文件路径的灵活写法
如图,我们看到的时html页面中静态文件的路径,其中/static/是settings.py中的设置: 假设我们将settings.py中的/static/改变了,这样的话我们还需要将html中的/s ...
- AttributeError: module 'select' has no attribute 'epoll'
AttributeError: module 'select' has no attribute 'epoll' 今天乌班图镜像莫名其妙损坏了,只好用Windows写并发TCP服务器的代码.运行后 ...
- jedis参数不当引发的问题总结
jedis参数不当引发dubbo服务线程池耗尽异常 现象:一个dubbo服务偶发性的出现个别机器甚至整个集群大量报线程池耗尽的问题.一开始对问题的处理比较粗暴,直接增加了10倍的线程数.但是问题依然偶 ...