使用selenium动态爬取
来源:互联网 发布:瑞典帅哥成灾 知乎 编辑:程序博客网 时间:2024/05/22 15:57
支持动态爬取,如登录、Javascript内容。
一、准备:
1、下载selenium(如:selenium-java-2.37.0.zip,约24MB),chromedriver.exe(chromedriver_win32.zip,约2.4MB)
https://sites.google.com/a/chromium.org/chromedriver/downloads
2、设置driver,参考:
http://download.csdn.net/detail/qianaier/7966945
二、开发:
连接例子
System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");WebDriver driver = new ChromeDriver();
登录例子
driver.get("https://xx.xxx.xx/admin/login.jsp");driver.findElement(By.id("input_username")).clear();driver.findElement(By.id("input_username")).sendKeys("username");driver.findElement(By.id("input_password")).clear();driver.findElement(By.id("input_password")).sendKeys("password");// log ondriver.findElement(By.className("btn")).click();System.out.println(driver.getTitle());
子元素访问例子(By.id/className/tagName)
WebElement sub = driver.findElement(By.id("accordion-element"));List<WebElement> subList = sub.findElement(By.className("accordion-inner")).findElement(By.className("nav-list")).findElements(By.tagName("li"));for (WebElement li:subList) {WebElement href = li.findElement(By.tagName("a"));
属性、文本内容
if (div1.getAttribute("style").contains("margin-bottom: 10px;")) {// detailSystem.out.println(div1.findElement(By.tagName("div")).getText());
内部页面跳转
WebDriver frameDriver = driver.switchTo().frame("iframe_1");
隐藏元素
-- 不能获取的例子
if (closeButton.getText().equals("关闭")) {closeButton.click();}
-- 可以获取的例子
if (closeButton.getAttribute("innerText").equals("关闭")) {((JavascriptExecutor) frameDriver).executeScript(""+ "document.getElementsByClassName('modal')[0]."+ "getElementsByClassName('btn')[0].click()");}
机器操作太快引起元素找不到
// 加上sleeptry {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}closeButton.click();
退出
frameDriver.quit();driver.quit();
Reference
Selenium定位不到元素的解决方法—iframe挡住了去路 http://www.51testing.com/html/02/n-855802.html
selenium webdriver 学习总结-浏览器启动方式 http://blog.csdn.net/pugongying1988/article/details/14525013
使用Selenium来抓取动态加载的页面 http://my.oschina.net/flashsword/blog/147334
Selenium Test 自动化测试 入门级学习笔记 http://www.renren.com
隐藏元素 http://my.oschina.net/longtutengfei/blog/166773,http://www.open-open.com/lib/view/open1402750704931.html
- 使用selenium动态爬取
- 使用selenium爬取百度文库文章(动态)
- selenium+ phantomjs实现动态网页爬取
- selenium+ Phantomjs爬取动态网页
- Selenium + phantomJS 爬取动态网站
- 使用selenium的webdriver动态爬取网页时遇到的问题
- 使用python + selenium爬取淘宝商品信息
- selenium在爬取中的使用
- 动态网页爬取例子(WebCollector+selenium+phantomjs)
- selenium+webdriver爬取动态网页介绍_python
- 用python+selenium+firefox爬取动态网页
- 基于Python,Selenium和PhantomJS实现动态页面爬取
- 基于Python3的phantomJs+Selenium动态网页爬取技术
- python+selenium+PhantomJS爬取网页动态加载内容
- 利用selenium爬取Axaj和动态网页
- 使用python+selenium爬取学生信息并入库
- python 使用selenium+urllib爬取淘宝MM照片
- Python使用Selenium爬取淘宝异步加载的数据
- linkLabel事件
- 小白鼠试毒酒的升级版
- 【struts2】五大核心jar包 和 作用
- LeetCode解题报告--Merge Two Sorted Lists
- js中的for和for-in
- 使用selenium动态爬取
- Android中的ArrayAdapter
- 快速排序算法实施尾递归优化
- Android、Android Studio 操作记录
- 简单的对list进行分页
- 编译原理学习之运行时数据区的管理
- Oracle实现对查询结果每N条再次分组
- bootstrap3 文件上传插件fileinput中文说明文档
- php时间控制