GETTING STARTED WITH WEBDRIVER
Selenium supports automation of all the major browsers in the market through the use of WebDriver. WebDriver is an API and protocol that defines a language-neutral interface for controlling the behaviour of web browsers. Each browser is backed by a specific WebDriver implementation, called a *driver*. The driver is the component responsible for delegating down to the browser, and handles communication to and from Selenium and the browser.
This separation is part of a conscious effort to have browser vendors take responsibility for the implementation for their browsers. Selenium makes use of these third party drivers where possible, but also provides its own drivers maintained by the project for the cases when this is not a reality.
The Selenium framework ties all of these pieces together through a user-facing interface that enables the different browser backends to be used transparently, enabling cross-browser and cross-platform automation.
CONSUMER BROWSERS
The Selenium framework officially supports the following browsers:
Browser
|
Maintainer
|
Versions Supported
|
Chromium
|
|
All versions
|
Firefox
|
|
54 and newer
|
Internet Explorer
|
Selenium
|
6 and newer
|
Opera
|
|
10.5 and newer
|
Safari
|
|
10 and newer
|
SPECIALISED BROWSERS
There is also a set of specialized browsers out there typically used in development environments. We can make use of some of these browsers for automation purposes also, and Selenium ties in support for the following specialized drivers:
Driver Name
|
Purpose
|
Maintainer
|
PhantomJSDriver
|
Headless PhantomJS browser backed by QtWebKit.
|
|
HtmlUnitDriver
|
Headless browser emulator backed by Rhino.
|
Selenium project
|
OTHER THIRD PARTY DRIVERS AND PLUGINS
Selenium can be extended through the use of plugins. Here are a number of plugins created and maintained by third parties. For more information on how to create your own plugin or have it listed, consult the docs.
Please note that these plugins are not supported, maintained, hosted, or endorsed by the Selenium project. In addition, be advised that the plugins listed below are not necessarily licensed under the Apache License v.2.0. Some of the plugins are available under another free and open source software license; others are only available under a proprietary license. Any questions about plugins and their license of distribution need to be raised with their respective developer(s).
LOCATING ELEMENTS
Locating one element
One of the most fundamental techniques to learn when using WebDriver is how to find elements on the page. WebDriver offers a number of built-in selector types, amongst them finding an element by its ID attribute:
WebElement cheese = driver.findElement(By.id("cheese"));
As seen in the example, locating elements in WebDriver is done on the WebDriver instance object. The findElement(By) method returns another fundamental object type, the WebElement.
- WebDriver represents the browser
- WebElement represents a particular DOM node (a control, e.g. a link or input field, etc.)
Once you have a reference to a web element that's been “found”, you can narrow the scope of your search by using the same call on that object instance:
WebElement cheese = driver.findElement(By.id("cheese")); WebElement cheddar = cheese.findElement(By.id("cheddar"));
You can do this because both the WebDriver and WebElement types implement the
SearchContextinterface. In WebDriver, this is known as a role-based interface. Role-based interfaces allow you to determine whether a particular driver implementation supports a given feature. These interfaces are clearly defined and try to adhere to having only a single role of responsibility. You can read more about WebDriver's design and what roles are supported in which drivers in the [Some Other Section Which Must Be Named](#).
Consequently, the By interface used above also supports a number of additional locator strategies. A nested lookup might not be the most effective cheese location strategy since it requires two separate commands to be issued to the browser; first searching the DOM for an element with ID “cheese”, then a search for “cheddar” in a narrowed context.
To improve the performance slightly, we should try to use a more specific locator: WebDriver supports looking up elements by CSS locators, allowing us to combine the two previous locators into one search:
driver.findElement(By.cssSelector("#cheese #cheddar"));
Locating multiple elements
It's possible that the document we are working with may turn out to have an ordered list of the cheese we like the best:
<ol id=cheese> <li id=cheddar>… <li id=brie>… <li id=rochefort>… <li id=camembert>… </ul>
Since more cheese is undisputably better, and it would be cumbersome to have to retrieve each of the items individually, a superior technique for retrieving cheese is to make use of the pluralized version findElements(By). This method returns a collection of web elements. If only one element is found, it will still return a collection (of one element). If no elements match the locator, an empty list will be returned.
List<WebElement> muchoCheese = driver.findElements(By.cssSelector("#cheese li"));
Element selection strategies
There are eight different built-in element location strategies in WebDriver:
Locator
|
Description
|
class name
|
Locates elements whose class name contains the search value (compound class names are not permitted)
|
css selector
|
Locates elements matching a CSS selector
|
id
|
Locates elements whose ID attribute matches the search value
|
name
|
Locates elements whose NAME attribute matches the search value
|
link text
|
Locates anchor elements whose visible text matches the search value
|
partial link text
|
Locates anchor elements whose visible text partially matches the search value
|
tag name
|
Locates elements whose tag name matches the search value
|
xpath
|
Locates elements matching an XPath expression
|
Tips on using selectors
In general, if HTML IDs are available, unique, and consistently predictable, they are the preferred method for locating an element on a page. They tend to work very quickly, and forego much processing that comes with complicated DOM traversals.
If unique IDs are unavailable, a well-written CSS selector is the preferred method of locating an element. XPath works as well as CSS selectors, but the syntax is complicated and frequently difficult to debug. Though XPath selectors are very flexible, they're typically not performance tested by browser vendors and tend to be quite slow.
Selection strategies based on link text and partial link text have drawbacks in that they only work on link elements. Additionally, they call down to XPath selectors internally in WebDriver.
Tag name can be a dangerous way to locate elements. There are frequently multiple elements of the same tag present on the page. This is mostly useful when calling the findElements(By) method which returns a collection of elements.
The recommendation is to keep your locators as compact and readable as possible. Asking WebDriver to traverse the DOM structure is an expensive operation, and the more you can narrow the scope of your search, the better.
PERFORMING ACTIONS ON THE AUT
You can set an element's text using the sendKeys method as follows:
String name = "Charles"; driver.findElement(By.name("name")).sendKeys(name);
Some web applications use JavaScript libraries to add drag-and-drop functionality. The following is a basic example of dragging one element onto another element:
WebElement source = driver.findElement(By.id("source")); WebElement target = driver.findElement(By.id("target")); new Actions(driver).dragAndDrop(source, target).build().perform();
Clicking on an element
You can click on an element using the click method:
driver.findElement(By.cssSelector("input[type='submit']")).click();
Selenium通过使用WebDriver支持市场上所有主流浏览器的自动化。WebDriver是一个API和协议,它定义了一个与语言无关的接口,用于控制web浏览器的行为。每个浏览器都支持特定的WebDriver实现,称为* driver *。驱动程序是负责委托给浏览器的组件,并负责处理来自Selenium和浏览器的通信。
这种分离是有意识地让浏览器供应商为其浏览器的实现承担责任的一部分。Selenium在可能的情况下使用这些第三方驱动程序,但也会在项目不适用的情况下为项目提供自己的驱动程序。
Selenium框架通过面向用户的界面将所有这些组件连接在一起,从而使不同的浏览器后端能够透明地使用,实现跨浏览器和跨平台的自动化。
客户端浏览器
Selenium框架正式支持以下浏览器:
浏览器
|
维护者
|
支持的版本
|
铬
|
|
所有版本
|
火狐
|
|
54和更新
|
IE浏览器
|
硒
|
6和更新
|
歌剧
|
|
10.5和更新
|
苹果浏览器
|
|
10和更新
|
专业浏览器
在开发环境中,通常还有一组专门的浏览器。我们也可以将这些浏览器的一部分用于自动化目的,而Selenium则支持以下专业驱动程序:
驱动名称
|
目的
|
维护者
|
PhantomJSDriver
|
由QtWebKit支持的无头PhantomJS浏览器。
|
|
HtmlUnitDriver
|
Rhino支持的无头浏览器模拟器。
|
硒项目
|
其他第三方驱动程序和插件
selenium可以通过使用插件进行扩展。这里有一些由第三方创建和维护的插件。有关如何创建自己的插件或列出它的更多信息,请参阅文档。
请注意,这些插件不受Selenium项目支持,维护,托管或认可。另外,请注意,下面列出的插件不一定根据Apache许可证v.2.0获得许可。一些插件可以在另一个免费和开源软件许可下获得; 其他人只能在专有许可下使用。任何关于插件及其分发许可证的问题都需要与各自的开发人员一起提出。
定位元素
找到一个元素
使用WebDriver时学习的最基本技巧之一是如何在页面上查找元素。WebDriver提供了许多内置的选择器类型,其中包括通过ID属性查找元素:
WebElement cheese = driver.findElement(By.id("cheese"));
如示例所示,在WebDriver中定位元素是在WebDriver实例对象上完成的 。该findElement(By)方法返回另一个基本对象类型,WebElement。
- WebDriver 代表浏览器
- WebElement 表示特定的DOM节点(控件,例如链接或输入字段等)
一旦你有一个被“找到”的web元素的引用,你可以通过在该对象实例上使用相同的调用来缩小搜索范围:
WebElement cheese = driver.findElement(By.id("cheese")); WebElement cheddar = cheese.findElement(By.id("cheddar"));
您可以这样做,因为WebDriver和WebElement类型都实现了
SearchContext 接口。在WebDriver中,这被称为基于角色的界面。基于角色的接口允许您确定特定的驱动程序实现是否支持给定的功能。这些界面有明确的界定,并试图坚持只承担单一的责任。您可以阅读关于WebDriver设计的更多信息,以及[某些其他必须命名的部分](#)中哪些驱动程序支持哪些角色。
因此,上面使用的By接口也支持许多额外的定位器策略。嵌套查找可能不是最有效的奶酪定位策略,因为它需要向浏览器发出两个单独的命令; 首先在DOM中搜索ID为“cheese”的元素,然后在缩小的上下文中搜索“cheddar”。
为了稍微提高性能,我们应该尝试使用更具体的定位器:WebDriver支持通过CSS定位器查找元素,使我们能够将以前的两个定位器合并为一个搜索:
driver.findElement(By.cssSelector("#cheese #cheddar"));
定位多个元素
有可能我们正在使用的文档可能会列出我们最喜欢的奶酪的列表:
<ol id=cheese> <li id=cheddar>… <li id=brie>… <li id=rochefort>… <li id=camembert>… </ul>
由于更多的奶酪毫无疑问地更好,并且单独检索每个项目会是麻烦的,所以优选的用于取出奶酪的技术是利用复数形式findElements(By)。此方法返回一组Web元素。如果只找到一个元素,它仍然会返回一个集合(一个元素)。如果没有元素匹配定位符,则返回空列表。
List<WebElement> muchoCheese = driver.findElements(By.cssSelector("#cheese li"));
元素选择策略
WebDriver中有八种不同的内置元素位置策略:
定位方式
|
描述
|
class name
|
定位其类名包含搜索值的元素(复合类名不被允许)
|
CSS selector
|
定位匹配CSS选择器的元素
|
ID
|
找到ID属性与搜索值匹配的元素
|
名称
|
找到其名称属性与搜索值匹配的元素
|
链接文本
|
定位其可见文本与搜索值匹配的定位元素
|
部分链接文本
|
定位可见文本与搜索值部分匹配的定位元素
|
标签名
|
找到标签名称与搜索值匹配的元素
|
XPath的
|
找到与XPath表达式匹配的元素
|
使用选择器的一些提示
一般来说,如果HTML ID可用,唯一且始终可预测,那么它们是在页面上定位元素的首选方法。它们往往工作得很快,并且放弃了复杂的DOM遍历所带来的大量处理。
如果唯一ID不可用,则写入良好的CSS选择器是定位元素的首选方法。XPath和CSS选择器一样工作,但语法复杂且经常难以调试。尽管XPath选择器非常灵活,浏览器厂商通常不会对它们进行性能测试,而且速度会很慢。
基于链接文本和部分链接文本的选择策略具有缺陷,因为它们仅在链接元素上工作。此外,他们在WebDriver内部调用XPath选择器。
标记名称可能是定位元素的一种危险方法。页面上经常存在多个相同标签的元素。但这在调用findElements(By)方法查找返回元素集合时非常有用。
建议将您的定位器尽可能紧凑和可读。让WebDriver遍历DOM结构是一项代价昂贵的操作,越能缩小搜索范围,效果越好。
在AUT上执行操作(元素的动作)
您可以使用sendKeys方法设置元素的文本,如下所示:
String name = "Charles"; driver.findElement(By.name("name")).sendKeys(name);
一些Web应用程序使用JavaScript库来添加拖放功能。以下是将一个元素拖到另一个元素上的基本示例:
WebElement source = driver.findElement(By.id("source")); WebElement target = driver.findElement(By.id("target")); new Actions(driver).dragAndDrop(source, target).build().perform();
点击一个元素
您可以使用click方法单击一个元素:
driver.findElement(By.cssSelector("input[type='submit']")).click();
- AutoIt实现Webdriver自动化测试文件上传
在运用WebDriver进行自动化测试时,由于WebDriver自身的限制,对于上传文件时Windows弹出的文件选择窗口无法控制,通过在网上查找资料锁定使用AutoIt来控制文件上传窗口. Auto ...
- webdriver学习笔记
该篇文章记录本人在学习及使用webdriver做自动化测试时遇到的各种问题及解决方式,问题比较杂乱.问题的解决方式来源五花八门,如有疑问请随时指正一遍改正. 1.WebDriver入门 //webdr ...
- Selenium WebDriver Code
Selenium WebDriver 用于模拟浏览器的功能,可以做网站测试用,也可以用来做crawler.我是用eclipse开发的,导入selenium-server-standalone-***. ...
- 使用httpclient 调用selenium webdriver
结合上次研究的selenium webdriver potocol ,自己写http request调用remote driver代替selenium API selenium web driver ...
- Selenium FF WebDriver 加载firebug 和设置代理
首先这次使用的webDriver for Firefox的 由于项目的原因,需要在测试的时候加载Firebug和使用vpn,加载代理 Firefox 加载代理,可以从FF菜单上看,代理分为好几种 我这 ...
- selenium webdriver 右键另存为下载文件(结合robot and autoIt)
首先感谢Lakshay Sharma 大神的指导 最近一直在研究selenium webdriver右键菜单,发现selenium webdriver 无法操作浏览器右键菜单,如图 如果我想右键另存为 ...
- 基于webdriver的jmeter性能测试-通过jmeter实现jar录制脚本的性能测试
续接--基于webdriver的jmeter性能测试-Eclipse+Selenium+JUnit生成jar包 在进行测试前先将用于支持selenium录制脚本运行所需的类包jar文件放到jmeter ...
- 输入框三种输入方式(selenium webdriver 干货)
在机票预定的页面,输入出发城市和到达城市输入框的时候, 发现直接使用sendkeys不好使, 大部分情况出现输入某城市后没有输入进去, 经过几天的研究,发现可以采取三种方式: 1. 先点击输入框,待弹 ...
- WebDriver API元素的定位
一.以下截图为用FireBug定位的用火狐(Firefox)浏览器打开的百度首页,下面所讲述的八种定位方法,就是以该截图中的百度输入框为例子. ①.FireBug是Firefox浏览器下的开发类插件, ...
- Selenium WebDriver 3.0 需要注意的事项
以下所有代码基于Java 首先,要使用WebDriver 3.0 的话 请使用JAVA 8(必要) 其次,由于W3C标准化以及各大浏览器厂商的积极跟进,自WebDriver 3.0 之后,Sele ...
随机推荐
- css 基础 选择器的使用
1.标签选择器解释:是针对一个标签做的样式,它会将匹配的所有标签都发生改变语法格式:标签名{ css样式代码 }2.类选择器特点:1.可以给某一个标签标记为一类,设定css样式,比较灵活 2.类目可以 ...
- EasySharding.EFCore 如何设计使用一套代码完成的EFCore Migration 构建Saas系统多租户不同业务需求且满足租户自定义分库分表、数据迁移能力?
下面用一篇文章来完成这些事情 多租户系统的设计单纯的来说业务,一套Saas多租户的系统,面临很多业务复杂性,不同的租户存在不同的业务需求,大部分相同的表结构,那么如何使用EFCore来完成这样的设计呢 ...
- 关于java的Excel导入导出之easypoi
导入easypoi相关jar包,这里的easypoi-base的包也可以不倒入,因为easypoi-web中有依赖easypoi-base会自动导入的 <!-- https://mvnrepos ...
- Linux命令查看各端口号占用情况
yum -y install net-tools netstat -ntpl
- Java定时调度
一.实现方式 Timer:单线程,串行: ScheduledExecutor:并行,论询,实现麻烦: Spring Scheduler:适合小任务: JcronTab:按照crontab语法编写的ja ...
- 创客系列教程——认识LED灯
认识LED灯 一.初识LED灯 LED灯是一种能够将电能转化为可见光的固态的半导体器件,它可以直接把电转化为光.LED灯逐步融入到生活中的方方面面:室内外的照明.电子指示牌.酷炫的舞台灯光.车辆的 ...
- TestNG中 ITestListener 的使用
1.关于testng中ITestListener 的相关介绍文档,请参考: http://javadox.com/org.testng/testng/6.8.7/org/testng/ITestLis ...
- Flowable实战(五)表单和流程变量
一.流程变量 流程实例按步骤执行时,需要保存并使用一些数据,在Flowable中,这些数据称为变量(variable). 流程实例可以持有变量,称作流程变量(process variables ...
- [javaweb]strut2-001漏洞分析
Strut2-001 漏洞描述 框架解析JSP页面标签时会对用户输入的Value值获取,在获取对应的Value值中递归解析%{.}造成了二次解析,最终触发表达式注入漏洞,执行任意代码 影响版本 2.0 ...
- 【记录一个问题】opencl的clGetPlatformIDs()在cuda 9下返回-1001(找不到GPU平台)
如题:在cuda9, nvidia驱动版本 384.81 的环境下运行opencl程序,在clGetPlatformIDs()函数中返回-1001错误. 把环境更换为cuda 10,驱动版本410.1 ...