java用selenium库控制chrome
一.简介
selenium是浏览器自动化工具,可以通过程序来控制浏览器行为,这就是selenium!你可以用它做任何你想做的事情.很多时候人们用它测试web应用,但selenium的用途绝不仅限于此.selenium拥有大多数浏览器厂商的支持,它可以驱动大多数浏览器.selenium的原理很简单,浏览器本身就提供了自动化接口,selenium只是把这些接口封装了一下,以统一的形式来编程,不必每个浏览器编一套程序.
selenium-RemoteControl已经被slenium-WebDriver所替代,selenium-RC已经不鼓励使用了.selenium IDE是一个firefox插件,可以方便地录制用户操作,是一个可视化插件.
要想使用selenium控制浏览器,可能需要浏览器提供的相应的驱动程序,如chrome就需要chrome-driver.在selenium官网上提供了与selenium有关的第三方工具.http://docs.seleniumhq.org/download/
selenium对于firefox支持得最好,有一个firefox插件selenium IDE,这个插件只能在firefox下使用.
selenium的主要用途是测试软件,当然也可以干别的事.比如爬取需要手动输入验证码的网站.
selenium是用java语言编写的,但是提供java,C#,python,nodeJS等语言的调用接口,也有第三方实现的selenium接口.
selenium可以通过命令行方式交互式执行,也可以通过编写程序执行.
htmlUnit是一个用java语言编写的模拟浏览器,但是它不是真正的浏览器,它连个界面都没有,只是一个模拟的浏览器.它对于js和css支持的不够完善,功能上肯定比不上真正的浏览器,但是它速度快,有时候是非常有用的.关键是它是基于java的浏览器.在使用selenium时,浏览器就可以使用htmlUnit作为浏览器,它的优点就是速度快.
二.下载
1.下载chrome-webdriver
在selenium官网上的download页面中提供了chrome-webdriver的下载链接.
https://sites.google.com/a/chromium.org/chromedriver/downloads
如果这个链接失效了,请百度"selenium chrome"
如果不下载chrome-webdriver,而直接写chrome.exe的路径,会报错
[1020:6356:1004/173348:ERROR:cache_util_win.cc(20)] Unable to move the cache: 0 [1020:6356:1004/173348:ERROR:cache_util.cc(134)] Unable to move cache folder C:\Users\weidiao\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache to C:\Users\weidiao\AppData\Local\Google\Chrome\User Data\ShaderCache\old_GPUCache_000 [1020:6356:1004/173348:ERROR:cache_creator.cc(134)] Unable to create cache [1020:6356:1004/173348:ERROR:shader_disk_cache.cc(589)] Shader Cache Creation failed: -2
2.下载jar包
可以使用maven,selenium-server这个jar包依赖selenium-java这个jar包,selenium-java又依赖大量的其他库.使用maven可以省去许多时间.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MySel20Proj</groupId>
<artifactId>MySel20Proj</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.0.0-beta4</version>
</dependency>
</dependencies>
</project>
因为selenium-java依赖的库特别多,所以需要导入很多jar包.从官网上下载selenium-java,把解压后文件夹中的全部jar包导入即可开始编写java代码了.
三.第一个selenium程序
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\weidiao\\Desktop\\chromedriver_win32\\chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
webDriver.manage().window().maximize();
webDriver.get("http://www.baidu.com");
WebElement kw = webDriver.findElement(By.id("kw"));
kw.sendKeys("暗算");
WebElement su = webDriver.findElement(By.id("su"));
su.click();
//webDriver.close();
System.out.println("Hello World!");
}
运行这个程序,就会打开百度,输入"暗算",点击搜索按钮.
四.API简介
要看selenium api,不要看博客,直接去官网上的documents页面查看api.
1.等待某个条件完成
有时需要等待浏览器运行js结束之后,再分析html页面.new出来一个WebDriver对象,调用它的until(ExpectedCondition<>condition)函数.
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
2.获取元素
//通过id
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
//通过className
List<WebElement> cheeses = driver.findElements(By.className("cheese"));
//通过tagName
WebElement frame = driver.findElement(By.tagName("iframe"));
//通过name
WebElement cheese = driver.findElement(By.name("cheese"));
//通过linkText
<a href="http://www.google.com/search?q=cheese">cheese</a>
WebElement cheese = driver.findElement(By.linkText("cheese"));
//通过部分linkText
<a href="http://www.google.com/search?q=cheese">search for cheese</a>
WebElement cheese = driver.findElement(By.partialLinkText("cheese"));
//通过css
<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>
WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged"));//通过xpath,比较麻烦//通过javaScript
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]");
List<WebElement> labels = driver.findElements(By.tagName("label"));
List<WebElement> inputs = (List<WebElement>) ((JavascriptExecutor)driver).executeScript(
"var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels);
3.操作元素
一个WebElement可以对应html很多控件,如按钮,单选按钮(select和deselect,click),复选按钮(跟单选按钮差不多),表单(submit),文件上传可以进行上传文件(sendKeys)
除了WebElement还有Select,表示多选的下拉列表.
java用selenium库控制chrome的更多相关文章
- Java使用Selenium几个例子
零.姿势 Selenium分为两个版本:Selenium RC和Selenium Webdriver.现在用Selenium Webdriver比较多. Selenium是一套工具,而不仅仅是一个操纵 ...
- Python 中 selenium 库
目录 selenium 基础语法 一. 环境配置 1. 安装环境 2. 配置参数 3. 常用参数搭配 4. 分浏览器启动 二. 基本语法 1. 元素定位 2. 控制浏览器操作 3. 操作元素的方法 3 ...
- python爬虫---selenium库的用法
python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...
- 浅谈python中selenium库调动webdriver驱动浏览器的实现原理
最近学web自动化时用到selenium库,感觉很神奇,遂琢磨了一下,写了点心得. 当我们输入以下三行代码并执行时,会发现新打开了一个浏览器窗口并访问了百度首页,然而这是怎么做到的呢? from se ...
- Java 框架、库和软件的精选列表(awesome java)
原创翻译,原始链接 本文为awesome系列中的awesome java Awesome Java Java 框架.库和软件的精选列表 项目 Bean映射 简化 bean 映射的框架 dOOv - 为 ...
- 为采集动态网页安装和测试Python Selenium库
1. 引言上一篇<为编写网络爬虫程序安装Python3.5>中测试小例子对静态网页做了一个简单的采集程序,而动态网页因为需要动态加载js获取数据,所以使用urllib直接openurl已经 ...
- python利用selenium库识别点触验证码
利用selenium库和超级鹰识别点触验证码(学习于静谧大大的书,想自己整理一下思路) 一.超级鹰注册:超级鹰入口 1.首先注册一个超级鹰账号,然后在超级鹰免费测试地方可以关注公众号,领取1000积分 ...
- java.util.stream 库简介
Java Stream简介 Java SE 8 中主要的新语言特性是拉姆达表达式.可以将拉姆达表达式想作一种匿名方法:像方法一样,拉姆达表达式具有带类型的参数.主体和返回类型.但真正的亮点不是拉姆达表 ...
- selenium 定制启动 chrome 的选项
序 使用 selenium 时,我们可能需要对 chrome 做一些特殊的设置,以完成我们期望的浏览器行为,比如阻止图片加载,阻止JavaScript执行 等动作.这些需要 selenium的 Chr ...
随机推荐
- SharePoint 2013 图文开发系列之网站栏
网站栏的本质,就是一个xml的描述文件,所以创建过程,基本就是通过Feature部署一个Xml文件,然后修改Xml文件的网站栏描述. 1.添加新项目,选择SharePoint 2013 空项目,如下图 ...
- 联想A880 DIY 换触摸屏屏幕
今年初入手的Lenovo A880手机,由于摔坏了屏幕不过能正常显示,咨询了联想的售后,说触摸屏和显示屏是分离的,换触摸屏需要280左右 为发挥DIY的精神,准备自己来处理这个屏幕 第一步:购买屏幕, ...
- jQuery操纵DOM元素属性 attr()和removeAtrr()方法使用详解
jQuery操纵DOM元素属性 attr()和removeAtrr()方法使用详解 jQuery中操纵元素属性的方法: attr(): 读或者写匹配元素的属性值. removeAttr(): 从匹配的 ...
- 浅谈RecyclerView(完美替代ListView,GridView)
Android RecyclerView 是Android5.0推出来的,导入support-v7包即可使用. 个人体验来说,RecyclerView绝对是一款功能强大的控件. 首先总结下Recycl ...
- (一)Maven初步了解与认识
Apache Maven是一个软件项目管理的综合工具.基于项目对象模型(POM)的概念,提供了帮助管理构建.文档.报告.依赖.发布等方法,Maven简化和标准化项目建设过程.处理编译,分配,文档,团队 ...
- [css]我要用css画幅画(五)
接着之前的[css]我要用css画幅画(四), 这次我给小明和静静增加了对话,用了简单的动画效果. github:https://github.com/bee0060/Css-Paint , 完整代码 ...
- crontab -e 每天定时备份mysql
contab -e 00 03 * * * mysqldump -u juandx --password=wenbin -d 'juandx$blog' -h host > /home/juan ...
- Ignite 配置更新Oracle JDBC Drive
如果使用Oracle 12C 作为Ignite 的Repository的话,在Repository Createion Wizard的配置过程中,会出现ORA-28040:No matc ...
- Bw树:新硬件平台的B树(内存数据库中的b树索引)
Bw树:新硬件平台的B树 Bw树:新硬件平台的B树 1. 概述 1.1 原子记录存储(Atomic Record Stores) 1.2 新的环境 1.3 实现 2 Bwtree的体系结构 2.1 现 ...
- Mysql时间类型处理
关于Mysql中时间的处理 最近在读<人类简史>,读第二遍.只有晚上睡觉之前读一点点,有时候觉得一天可以抽出一个专门的时间来看书了,效率应该能高不少. 另外分享个网址可以随心创作 这里有一 ...