E开始使用webdriver

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.
More details about drivers can be found in Driver Idiosyncracies.
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
Opera Chromium / Presto
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).
Browser
Latest version
     
2.29 - 2017-04-04
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框架通过面向用户的界面将所有这些组件连接在一起,从而使不同的浏览器后端能够透明地使用,实现跨浏览器和跨平台的自动化。
有关驱动程序的更多详细信息可以在Driver Idiosyncracies中找到 。
客户端浏览器
Selenium框架正式支持以下浏览器:
浏览器
维护者
支持的版本
所有版本
火狐
54和更新
IE浏览器
6和更新
歌剧
Opera Chromium / Presto
10.5和更新
苹果浏览器
10和更新
专业浏览器
在开发环境中,通常还有一组专门的浏览器。我们也可以将这些浏览器的一部分用于自动化目的,而Selenium则支持以下专业驱动程序:
驱动名称
目的
维护者
PhantomJSDriver
由QtWebKit支持的无头PhantomJS浏览器。
HtmlUnitDriver
Rhino支持的无头浏览器模拟器。
硒项目
其他第三方驱动程序和插件
selenium可以通过使用插件进行扩展。这里有一些由第三方创建和维护的插件。有关如何创建自己的插件或列出它的更多信息,请参阅文档。
请注意,这些插件不受Selenium项目支持,维护,托管或认可。另外,请注意,下面列出的插件不一定根据Apache许可证v.2.0获得许可。一些插件可以在另一个免费和开源软件许可下获得; 其他人只能在专有许可下使用。任何关于插件及其分发许可证的问题都需要与各自的开发人员一起提出。
浏览器
最新版本
     
2.29 - 2017-04-04
定位元素
找到一个元素
使用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"));
您可以这样做,因为WebDriverWebElement类型都实现了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();
 
posted @ 2019-06-04 19:48  HenryZ.Tang  阅读(244)  评论(0编辑  收藏  举报