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浏览器.
void org.openqa.selenium.WebDriver.get(String url)
打开一个网站.
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属性寻找.


org.openqa.selenium.WebElement
接口。对应于dom中的标签.
String org.openqa.selenium.WebElement.getText()
获取标签及子标签的文本内容.
String org.openqa.selenium.WebElement.click()
触发指定元素的单击事件。

4.示例代码及工程

代码:
图4-1 根据name属性让selenium定位标签



0 0
原创粉丝点击