一.简介

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"?>
<projectxmlns="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 voidmain(String[] args) {System.setProperty("webdriver.chrome.driver","C:\\Users\\weidiao\\Desktop\\chromedriver_win32\\chromedriver.exe");WebDriver webDriver= newChromeDriver();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出来一个WebDriverWait对象,调用它的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>() {publicBoolean 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,表示多选的下拉列表.

转载于:https://www.cnblogs.com/weiyinfu/p/5930468.html

java用selenium库控制chrome相关推荐

  1. 【java】Selenium爬虫控制谷歌浏览器

    每逢佳节倍思亲,中秋节到了,这里祝愿大家中秋节快乐. 今天主要向大家介绍的是如何自动打开浏览器并自动搜索指定内容. 1. 下载谷歌浏览器驱动 为什么要下载驱动? 因为一般的写爬虫的方法是用脚本直接对目 ...

  2. 遇到python调用selenium库使用chrome时候报错 selenium.common.exceptions.WebDriverException问题

    遇到selenium.common.exceptions.WebDriverException问题 selenium.common.exceptions.WebDriverException: Mes ...

  3. Java使用Selenium几个例子

    零.姿势 Selenium分为两个版本:Selenium RC和Selenium Webdriver.现在用Selenium Webdriver比较多. Selenium是一套工具,而不仅仅是一个操纵 ...

  4. 【Selenium】控制当前已经打开的 chrome浏览器窗口(高级版)

    前言 利用 Selenium 获取已经打开的浏览器窗口,全python操作 标题 链接 [Selenium]控制当前已经打开的 chrome浏览器窗口 https://blog.csdn.net/we ...

  5. 【Selenium】控制当前已经打开的 chrome浏览器窗口

    前言 有过几个小伙伴问过我如何利用 Selenium 获取已经打开的浏览器窗口,这里给安排了,还安排了两篇. 标题 链接 [Selenium]控制当前已经打开的 chrome浏览器窗口 https:/ ...

  6. java中selenium设置chrome浏览器为开发者模式与IP代理

    java中selenium设置chrome浏览器为开发者模式与IP代理 前言 代码 前言 之前为公司写了个爬虫项目,有时候为了绕过网站的反爬机制需要用到ip代理以及设置浏览器为开发者模式,看了一些se ...

  7. python中webdriver_浅谈python中selenium库调动webdriver驱动浏览器的实现原理

    最近学web自动化时用到selenium库,感觉很神奇,遂琢磨了一下,写了点心得. 当我们输入以下三行代码并执行时,会发现新打开了一个浏览器窗口并访问了百度首页,然而这是怎么做到的呢? 1 from ...

  8. python selenium 用法 和 Chrome headless

    From: http://cuiqingcai.com/2599.html Selenium教程:https://www.yiibai.com/selenium selenium 官方参考文档:htt ...

  9. Python Selenium库的使用

    (一)Selenium基础 入门教程:Selenium官网教程 1.Selenium简介 Selenium是一个用于测试网站的自动化测试工具,支持各种浏览器包括Chrome.Firefox.Safar ...

最新文章

  1. 解决mysql java.sql.SQLException: The server time zone value‘XXXXXX' is unrecognized or represents...
  2. uniapp 自定义进度条_如何解决uniapp小程序下载进度条问题
  3. 大连开发区取暖费能微信支付吗_下半年教资报考人数增加,那到底能不能异地报考呢?...
  4. 阶段项目:学生信息管理系统数据库设计
  5. 计算机毕业设计php校园餐厅网上订餐系统
  6. 法拉科机器人编程软件_发那科机器人编写简单的程序教程
  7. Phase2 Day3 List
  8. android SoundPool例子,Android SoundPool即时音效的使用Demo
  9. 音视频开发:多播系统中RTP如何工作?
  10. COMPASS数据上报总结
  11. python阿拉伯数字转中文_阿拉伯数字转换成中文的python代码
  12. 某finecms的csrf漏洞(CVE-2018-18191)
  13. PyCharm配置SSH和SFTP连接远程服务器
  14. 微博营销的价值和优缺点
  15. 数据分析师课程(ArcGIS按属性选数据、各种数据分析案例)
  16. #华为模拟器eNSP
  17. SQL查询语句-练习01+答案(含截图)
  18. web编程技术的知识点---HTML
  19. 还搞不懂RS485?18个问答彻底讲明白RS485
  20. 卡耐基《人性的优点》摘录

热门文章

  1. JS事件冒泡和事件捕获
  2. dubbo与zookeeper的关系
  3. iOS中利用UISearchBar实现搜索
  4. Centos 7 环境下,如何使用 Apache 实现 SSL 虚拟主机 双向认证 的详细教程:
  5. 如何解决软键盘弹出引起的各种不适
  6. 如何让web控件FileUpload选择完文件之后就自动触发事件,让Image控件显示出图片来...
  7. Python用20行代码实现完整邮件功能 [完整代码+建议收藏]
  8. mysql里有sqlfront_使用SQL-Front启动MySQL8.0报错
  9. mysql的请求分发,基于 gorilla/mux 实现路由匹配和请求分发:服务单页面应用
  10. 在matlab环境中实现图像的傅里叶变换,matlab用傅里叶变换实现图像的低通滤波