Selenium webdriver can simulate the operation of browser on the premise of location of interface elements. Location of elements can be said to be the basis of Selenium automation scripts. In this section, the author will introduce how to locate elements in selenium.
Method of locating elements
Selenium provides the following methods for locating elements:
First, look at an HTML file test_page.
<html> <body> <form class="form-test" name="register" action="success.html" method="post"> <h3>Registered account number</h3> <a href="/home">Home Page</a> <table bgcolor="aqua"> <tr> <td>Nickname?</td> <td><input id="input username" type="text" name="username" class="input"></td> </tr> <tr> <td>Password:</td> <td><input type="text" name="password"></td> </tr> <tr> <td>Confirm password:</td> <td><input type="text" name="confPassword"></td> </tr> <tr> <td>Gender:</td> <td> <input type="radio" name="sex" value="man" checked>male <input type="radio" name="sex" value="woman">female </td> </tr> </table> </form> </body> </html>
1. id
Use the id of the element to locate the username input box.
require 'selenium-webdriver' dr = Selenium::WebDriver.for :chrome dr.get "http://test.html" # by id dr.find_element(:id, 'input username').click
2.name
The name attribute of the element is used to locate the username input box.
require 'selenium-webdriver' dr = Selenium::WebDriver.for :chrome dr.get "http://test.html" # by name dr.find_element(:name, 'username').click
3. class name
The class attribute of the element is used to locate the username input box.
require 'selenium-webdriver' dr = Selenium::WebDriver.for :chrome dr.get "http://test.html" # by name dr.find_element(:class, 'input').click
4. link text and partial link text
The location of Home Page links is accomplished by the text attribute of the link element.
require 'selenium-webdriver' dr = Selenium::WebDriver.for :chrome dr.get "http://test.html" # by link_text link_text = dr.find_element(:link_text, 'Home Page').get_text puts link_text # by partial_link_text link_text = dr.find_element(:partial_link_text, 'Home').get_text puts link_text
5. tag name
Location of header elements by tag name
require 'selenium-webdriver' dr = Selenium::WebDriver.for :chrome dr.get "http://test.html" # by tag name header_text = dr.find_element(:tag_name, 'h3').get_text puts header_text
6. xpath
XPath is a general method for locating elements in HTML documents. It has its own grammar rules and supports various functions. It can be said that XPath is the most comprehensive method for locating elements in HTML documents. In the process of developing Selenium automation use cases, the most I use is xpath.
Here, only a simple example is given to illustrate the positioning use of xpath. The specific use of XPath will be introduced in another topic.
require 'selenium-webdriver' dr = Selenium::WebDriver.for :chrome dr.get "http://test.html" # by xpath dr.find_element(:xpath, "//tr/td/input[@value='women']").click
7. css selector
css selector, like xpath, is a very powerful location method. But unlike xpath, css selector can only support backward positioning, while XPath can support forward and backward positioning.
The following example shows how to locate the same element in the xpath example with css selector.
require 'selenium-webdriver' dr = Selenium::WebDriver.for :chrome dr.get "http://test.html" # by css selector dr.find_element(:css, "tr>td>input[value='women']").click
Locate a set of elements
Selenium supports simultaneous positioning of a set of elements, which is very useful when dealing with multiple options or table elements.
require 'selenium-webdriver' dr = Selenium::WebDriver.for :chrome dr.get "http://test.html" # Get number of items num_of_tds = dr.find_elements(:tag_name, "td").count