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 ...
随机推荐
- MySQL5.7 Replication主从复制配置教程
最近配置mysql5.7主从复制的时候碰到了些问题,老老实实按老版本的步骤配置会有错误,后来自己查看了官方文档,才解决了问题,在这里总结一下5.7的配置步骤, 大体步骤跟老版本的还是一样的,只是有一些 ...
- post、get的区别
get的参数会显示在浏览器地址栏中,而 post的参数不会显示在浏览器地址栏中: 使用 post提交的页面在点击[刷新]按钮的时候浏览器一般会提示“是否重新提交”,而 get则不会: 用get的页面可 ...
- Java中的LookAndFeel
Java'中的几种Look and Feel1.Metal风格 (默认)String lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFe ...
- css外边距合并和z-index的问题
参考这篇文章, 将外边距的 折叠 参考这篇文章, 将bfc的生成, bfc的应用 参考这篇文章 position: absolute的元素, 仍然具有内填充padding和border边框属性样式, ...
- Linux内核循环链表经典分析和移植
为什么说这个链表做的经典呢,哥哥我从Linux内核里边儿扣出来的,要么怎么说内核不是一般人能写的,这代码太TM优美了! 这里有一篇参考文章:http://isis.poly.edu/kulesh/st ...
- JQuery仿淘宝商家后台管理 之 管理添加分类
先看一下效果图: 实现功能: 1.打开时加载分类信息,折叠所有分类 2.动态添加子类和父类 3.顺序的调整 4.无刷新删除,添加 5.保存到数据库 下面是HTML代码 : <title>分 ...
- 微信封号浪潮再起 A货假代购还能在朋友圈泛滥多久?
你的微信朋友圈是不是很活跃?是不是被很多所谓的名品所包围?没错,这些很多都是A货或假代购的伎俩.如果xmyanke的微信朋友圈出现这些东东,我就会直接屏蔽他的朋友圈权限.具体方法是:打开他的微信详细资 ...
- Ubuntu 14 安装Java(JRE、JDK)
JRE vs OpenJDK vs Oracle JDK JRE(Java Runtime Environment),它是你运行一个基于Java语言应用程序的所正常需要的环境.如果你不是一个程序员的话 ...
- JAVA-多屏幕显示
以下代码适用于:一台主机连接多台显示器,JAVA Swing窗口需要分别显示到对应的显示器上. GraphicsEnvironment env = GraphicsEnvironment.getLoc ...
- 给Storyboard设置初始页面(Initial Controller)
原文:https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/Chapters/SetInitialContr ...