Selenium Practical Tutorial Series (2)---Element Positioning

The premise that Selenium webdriver can simulate the human operation on the browser is the positioning of the interface elements. The positioning 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.

methods for locating elements

Selenium provides the following methods of locating elements: First look at an HTML file test_page.:

<html>
    <body>
        <form class="form-test" name="register" action="success.html" method="post">
    <h3>注册账号</h3>
    <a href="/home">Home Page</a>
    <table bgcolor="aqua">
        <tr>
            <td>昵称:</td>
            <td><input id="input username" type="text" name="username" class="input"></td>
        </tr>
        <tr>
            <td>密码:</td>
            <td><input type="text" name="password"></td>
        </tr>
        <tr>
            <td>确认密码:</td>
            <td><input type="text" name="confPassword"></td>
        </tr>
        <tr>
            <td>性别:</td>
            <td>
                <input type="radio" name="sex" value="man" checked>男
                <input type="radio" name="sex" value="woman">女
                </td>
            </tr>
        </table>
    </form>
    </body>
 </html>

1. id

The positioning of the username input box is completed by the element id.

require 'selenium-webdriver'
dr = Selenium::WebDriver.for :chrome
dr.get "http://test.html"

# by id
dr.find_element(:id, 'input username').click

2.name

nameThe positioning of the username input box is completed through the attributes of the element .

require 'selenium-webdriver'
dr = Selenium::WebDriver.for :chrome
dr.get "http://test.html"

# by name
dr.find_element(:name, 'username').click

3. class name

classThe positioning of the username input box is completed through the attributes of the element .

require 'selenium-webdriver'
dr = Selenium::WebDriver.for :chrome
dr.get "http://test.html"

# by name
dr.find_element(:class, 'input').click

4. link text和partial link text

The positioning of the Home Page link is done through 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

The location of the header element is completed by the name of the label

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 set of grammatical rules and supports various functions. It can be said to be the most comprehensive locating method. In the process of developing Selenium automation use cases, xpath is the most used. Here only a simple example is used to illustrate the use of xpath positioning, and 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

Like xpath, css selector is also a very powerful positioning 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 use css selector to locate the same element in the xpath example.

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

position a set of elements

Selenium supports obtaining the positioning of a group of elements at the same time, which is very useful when dealing with multiple option selections 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

Guess you like

Origin blog.csdn.net/nhb687095/article/details/130439740
Recommended