selenium 自动化web测试
来源:互联网 发布:nginx lua cookie 编辑:程序博客网 时间:2024/04/28 21:33
1.简介
用java抓取网页内容有很多种方法。
a)使用HttpClient发送请求,然后解析网页或json。
b)使用jsoup,来帮我们发请求,方便地像jquery那样抠取标签
c)使用selenium,通过浏览器加载页面。
1.1 selenium的优势
有些内容是通过ajax获取并通过js填充到dom中的,所以离开浏览器拿不到内容。
有些网站为防爬虫,用js做了特殊的运算与校验,然后服务端才会给正确的数据,所以离开浏览器拿不到正确的内容。
1.2 selenium的劣势
启动浏览器会消耗较大的系统资源,效率不会太高。
2.使用方法
以windows7+chrome+java 平台作说明。
首先安装chrome浏览器。
然后下载chromeDriver——https://sites.google.com/a/chromium.org/chromedriver/
然后写java。
依赖见下:
<dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>2.42.2</version></dependency><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-chrome-driver</artifactId><version>2.42.2</version></dependency>
3.常用类与方法
org.openqa.selenium.chrome.ChromeDriver
此类负责启动chrome浏览器.
此类负责启动chrome浏览器.
void org.openqa.selenium.WebDriver.get(String url)
打开一个网站.
WebElement org.openqa.selenium.WebDriver.findElement(By by)
打开一个网站.
WebElement org.openqa.selenium.WebDriver.findElement(By by)
查找element.
List<WebElement> org.openqa.selenium.WebDriver.findElements(By by)
查找符合条件的多个element.
org.openqa.selenium.By
抽象类,它的一些静态方法用于定位element.
By org.openqa.selenium.By.name(String name)
根据标签的name属性寻找.
By org.openqa.selenium.By.tagName(String name)
根据标签名寻找.
By org.openqa.selenium.By.id(String id)
根据标签的id属性寻找.
By org.openqa.selenium.By.className(String className)
根据标签的class属性寻找.
List<WebElement> org.openqa.selenium.WebDriver.findElements(By by)
查找符合条件的多个element.
org.openqa.selenium.By
抽象类,它的一些静态方法用于定位element.
By org.openqa.selenium.By.name(String name)
根据标签的name属性寻找.
By org.openqa.selenium.By.tagName(String name)
根据标签名寻找.
By org.openqa.selenium.By.id(String id)
根据标签的id属性寻找.
By org.openqa.selenium.By.className(String className)
根据标签的class属性寻找.
org.openqa.selenium.WebElement
接口。对应于dom中的标签.
String org.openqa.selenium.WebElement.getText()
接口。对应于dom中的标签.
String org.openqa.selenium.WebElement.getText()
获取标签及子标签的文本内容.
String org.openqa.selenium.WebElement.click()
触发指定元素的单击事件。
String org.openqa.selenium.WebElement.click()
触发指定元素的单击事件。
4.示例代码及工程
代码:
图4-1 根据name属性让selenium定位标签
0 0
- selenium 自动化web测试
- selenium自动化测试web
- 利用 Selenium 自动化 web 测试
- 利用 Selenium 自动化 web 测试
- 利用 Selenium 自动化 web 测试
- selenium web自动化测试工具
- Selenium+Python:Web自动化测试
- python3+selenium,web自动化测试
- web 页面自动化测试工具--selenium
- Selenium进行web端的自动化测试
- selenium web的自动化测试工具
- 关于java web自动化测试工具 selenium
- Web自动化测试工具——Selenium
- web自动化测试框架(Selenium+TestNG)
- Web测试自动化的Selenium组件
- Web自动化测试三:selenium框架搭建
- selenium——Web自动化测试工具
- Selenium+Python Web自动化测试环境搭建
- opencv 随机数使用
- 数据持久化
- Java线程池ThreadPoolExecutor深度探索及源码解析
- 智能家居软件开发方向主要会用到些什么技术?
- Java中的异常和File类的使用
- selenium 自动化web测试
- 机器学习三要素
- css实现鼠标悬停时滑出层
- 详谈动态窗口下图像放大功能
- #define NFSTAT (*(volatile unsigned long *)0x4e000020) 与#define NFSTAT (*(volatile unsigned char *)
- 我的产品开发之旅(3) - 设计商品一级、二级分类、推荐商品表
- hibernate xml标签 id generator详解
- android.database.sqlite.SQLiteException: table tb_diary2 has no column named recordPath 解决办法
- netstat下time_wait状态的tcp连接