Selenium FF WebDriver 遍历所有链接(另类爬虫)
请看这个页面,我想要找到某个公告的内容,必须一个一个打开链接,尼玛好多啊。
于是,我机智的使用selenium打开每一个链接,然后把公告内容写入txt
那需要做一下步奏
1.依次打开一个公告
2.切换focus到新窗口,找到公告内容,写到txt
3.关闭该窗口
4.切换到主窗口
5.当前页面遍历完,点击下一页
6.重复步奏1
由于下一页是一个很好用的flag,就可以当做循环条件,因为最后一页没有下一页的element
接下来要找到相关的的xpath
列表数目: count(//tr/td/a[starts-with(@href,'article_show.asp?ID=') and @title!='' ])
列表: //tr/td/a[starts-with(@href,'article_show.asp?ID=') and @title!='' ]
下一页: //div/a[text()='下一页']
selenium WebDriver测试网页时,点击target=”_blank”的链接,在打开新页面,切换到新窗口的
这要使用
String currentWindow = driver.getWindowHandle();//获取当前窗口句柄
Set<String> handles = driver.getWindowHandles();//获取所有窗口句
WebDriver window = driver.switchTo().window(it.next());//切换到新窗口
driver.switchTo().window(currentWindow);//回到原来页面
driver=driver.switchTo().window(driver.getWindowHandle()); //把下一页变成当前driver
currentWindow = driver.getWindowHandle();
//get all windows
Set<String> handles= driver.getWindowHandles();
for (String s : handles)
{
//current page is don't close
if (s.equals(currentWindow))
continue;
else
{
window =driver.switchTo().window(s);
}
window .close() ;
}
driver.switchTo().window(currentWindow);
具体代码
package com.packt.webdriver.chapter3; import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; public class TraversalAllLinks {
private static String currentWindow; public static void main(String[] args) { WebDriver driver=DriverFactory.getFirefoxDriver();
driver.get("http://www.lhgtj.gov.cn/article.asp?ClassID=86&page=1"); driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
WebElement nextPage=driver.findElement(By.xpath("//tr/td/a[@title='下一页']"));
while(nextPage.isDisplayed())
{ List<WebElement> links=driver.findElements(By.xpath("//tr/td/a[starts-with(@href,'article_show.asp?ID=') and @title!='' ]")); for(WebElement link:links)
{
WebDriver window;
System.out.println(link.getText());
try {
writeToTXT(link.getText());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
link.click();
currentWindow = driver.getWindowHandle();
//get all windows
Set<String> handles= driver.getWindowHandles();
for (String s : handles)
{
//current page is don't close
if (s.equals(currentWindow))
continue;
else
{
window =driver.switchTo().window(s);
window.manage().window().maximize();
window.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
window.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
//get all tables
List<WebElement> tbs=window.findElements(By.xpath("//tbody/tr/td/p"));
for(WebElement tb:tbs)
{
System.out.println(tb.getText());
try {
writeToTXT(tb.getText()+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
//close the table window
window .close() ;
}
//swich to current window
driver.switchTo().window(currentWindow);
} }
// click next page
nextPage.click();
//set next page to current page
driver=driver.switchTo().window(driver.getWindowHandle());
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
nextPage=driver.findElement(By.xpath("//tr/td/a[@title='下一页']")); } }
//write logs
public static void writeToTXT(String message) throws IOException
{
BufferedWriter bf = null;
try {
//set true ,avoid
bf = new BufferedWriter(new FileWriter("report.txt", true));
bf.write(message);
bf.flush(); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
bf.close();
} } }
DriverFactory
public static WebDriver getFirefoxDriver()
{
try
{
WindowsUtils.tryToKillByName("firefox.exe");
}
catch(Exception e)
{
System.out.println("can not find firefox process");
}
File file=new File("d:\\firebug-2.0.4-fx.xpi");
FirefoxProfile profile = new FirefoxProfile(); try {
profile.addExtension(file);
profile.setPreference("extensions.firebug.currentVersion", "2.0.4");
profile.setPreference("extensions.firebug.allPagesActivation", "on");
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
} WebDriver driver = new FirefoxDriver(profile);
return driver; }
Selenium FF WebDriver 遍历所有链接(另类爬虫)的更多相关文章
- Selenium FF WebDriver运行时开启firebug的2种方式
上一次我实测FF webdriver 加载firefoxhttp://www.cnblogs.com/tobecrazy/p/3997375.html 那么问题就来了,既然能加载firebug能否在运 ...
- Selenium FF WebDriver 加载firebug 和设置代理
首先这次使用的webDriver for Firefox的 由于项目的原因,需要在测试的时候加载Firebug和使用vpn,加载代理 Firefox 加载代理,可以从FF菜单上看,代理分为好几种 我这 ...
- python selenium自动化点击页面链接测试
python selenium自动化点击页面链接测试 需求:现在有一个网站的页面,我希望用python自动化的测试点击这个页面上所有的在本窗口跳转,并且是本站内的链接,前往到链接页面之后在通过后退返回 ...
- Selenium的WebDriver API元素定位中的XPath和CSS
元素的定位和操作是自动化测试的核心部分,其中操作又是建立在定位的基础上的. 浏览器的常规操作 import time from selenium import webdriver # 打开浏览器 dr ...
- python使用selenium,webdriver自动下载百度网盘内容
想实现一个自动下载微信公众号分享百度网盘图片链接的爬虫,使用selenium和火狐的webdriver进行完成 1.首先根据自己的浏览器下载相应的webdriver驱动器,python中导入selen ...
- Selenium+PhantomJS实现简易有道翻译爬虫
Selenium一款自动化测试工具,当然用来写爬虫也是没有问题的.它支持Chrome.Safari.Firefox等主流界面式浏览器,另外它也支持多种语言开发,比如 Java,C,Ruby,Pytho ...
- Selenium:WebDriver简介及元素定位
参考内容:官方API文档,下载链接:http://download.csdn.net/detail/kwgkwg001/4004500 虫师:<selenium2自动化测试实战-基于python ...
- How to set Selenium Python WebDriver default timeout?
Trying to find a good way to set a maximum time limit for command execution latency in Selenium Pyth ...
- Selenium VS Webdriver
Selenium 是 ThroughtWorks 一个强大的基于浏览器的开源自动化测试工具,它通常用来编写 Web 应用的自动化测试.随着 Selenium 团队发布 Selenium 2(又名 We ...
随机推荐
- 使程序在Linux下后台运行
一.为什么要使程序在后台执行 我们计算的程序都是周期很长的,通常要几个小时甚至一个星期.我们用的环境是用putty远程连接到日本Linux服务器.所以使程序在后台跑有以下三个好处: 1:我们这边是否关 ...
- 【8-16】Android学习笔记01
Android目录树 Activity 生命周期 创建一个 Activity 在 android 中创建一个 Activity 要编写一个继承自 android.app.Activity的 Java ...
- DWZ框架中ajax提交文件表单的处理(关闭当前dialog + 刷新父级navTab)
先重点关注两个js文件:dwz.ajax.js和dwz.core.js 流程: 1.回调iframeCallback <form xx enctype="multipart/form- ...
- golang笔记——环境搭建
1.下载安装 从 https://golang.org/dl/ 这里下载最新版本的 golang 安装包,分别有 Windows\Linux\Apple OSX\源码包. golang的官方网站是 h ...
- HTML5+CSS3+jquery实现简单的音乐播放器
...最近天热的,感觉就像煎饼...然后别人在把妹子的时候,只有偶们这帮苦逼的程序员在那边撸代码...我日哦! 然后今天晒的是偶早年写的一个播放器...看上去是不是很有感觉的样子!一番宝物,Lisa唱 ...
- [译]通过IIS Request Filtering解决SQL Server CPU高的问题
原文http://www.peterviola.com/solving-sql-server-high-cpu-with-iis-request-filtering/ Top Queries by T ...
- Tomcat 6 --- 使用Jasper引擎解析JSP
熟悉JAVA web开发的朋友都知道JSP会被转换成java文件(预编译),然后编译成class使用,即按照JSP-->java-->class的过程进行编译. 由于JVM只认识class ...
- 【Solr】 solr对拼音搜索和拼音首字母搜索的支持
问:对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输入拼音和拼音首字母就给出商品的信息,怎么办呢? 实现方式有2种,但是他们其实是对应的. 用lucene实现 1.建索引, 多建一个索引字段 ...
- IIS服务器运行一段时间后卡死,且无法打开网站(IIS管理无响应,必须重启电脑)
问题描述: 公司希望使用IIS配合网站显示一些订单跟进的情况并展示出来,所以我们在一台演示的Win7 Pro电脑上安装了IIS,但使用了一段时间后发现每过几天页面就无法正常访问了,而且打开IIS管理器 ...
- plt和got
最近在学习linux高级调试技术.下面就动态库连接这块做了一个实验 首先理解下plt是procedure linkage table,got是global offset table.got表中存放的是 ...