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. maven环境搭建教程

    1.下载和解压 访问   maven.apache.org   .点击download 下载对应系统的文件,window下载bin的zip文件. maven需要java1.7jdk支持,如果jdk没有 ...

  2. JavaScript原生封装ajax请求和Jquery中的ajax请求

    前言:ajax的神奇之处在于JavaScript 可在不重载页面的情况与 Web 服务器交换数据,即在不需要刷新页面的情况下,就可以产生局部刷新的效果.Ajax 在浏览器与 Web 服务器之间使用异步 ...

  3. LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)

    82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...

  4. QPS和并发量

    QPS(q) :每秒处理的请求数量 并发量 (c):同时支持多少个用户在线.与服务器的请求处理模型有关,如果是BIO模型,则并发量就受限于最大能支持多少个线程,如果是NIO模型,则并发量与socket ...

  5. 【转帖】两种IO模式:Proactor与Reactor模式

    两种IO模式:Proactor与Reactor模式 https://www.cnblogs.com/pigerhan/p/3474217.html. 挺好的说明了epoll和IOCP的区别 在高性能的 ...

  6. Java开发笔记(一百二十七)Swing的标签

    提起AWT的标签控件Label,那个使用体验可真叫糟糕,不但不支持文字换行,而且对中文很不友好,既可能把中文显示为乱码,还不支持博大精深的各种中文字体.所幸Swing的升级版标签JLabel在各方面都 ...

  7. Python之序列化概念

    我们把对象(变量)从内存中变成可存储或运输的过程称之为序列化,在 Python 中叫 pickling ,在其他的语言中也被称之为 serialization,marshalling,flatteni ...

  8. 魔术方法之__call、__callStatic

    1.__call() 作用,当调用不存在的方法时,会调用该方法.实际应用,当程序调用不存在的方法时,意外导致程序终止. .或者当你调用了受保护的或者是私人的方法时,也会自动调用__call方法 结果: ...

  9. Windows 10部署教程

    1. 获取主板密钥 在powershell中执行: (Get-WmiObject -query 'select * from softwareLicensingService').OA3xOrigin ...

  10. go条件变量的使用和原理

    场景 最近写代码时碰到一个场景, 需要使用 map[int]struct{} 结构来存储task, map的key是task的id,随时可以增减.因为的确除了看书,基本上没使用过条件变量所以后面过了一 ...