Webdriver API (element positioning and manipulation)

Source: Internet
Author: User

Webdriver provides 8 ways to locate elements: ID, Name, class name, tag name, link text, Partial link text, XPath, CSS selector

One or more 8 elements positioning, Webdriver provides two sets of wording

1. Use by to locate the element: by means of a by to declare the location of the method, you need to introduce the by module

Find_element (by.id, "kw")

Find_element (By.name, "WD")

2. Direct positioning

find_element_by_id ("kw")

Find_element_by_name ("WD")

Two, 8 elements of the positioning

1. ID Positioning

The HTML rule id attribute must be unique in an HTML document.

find_element_by_id ("kw")

2. Name Locator

The property value of name may not be unique in the HTML page type.

Find_element_by_name ("WD")

3. Class positioning

To position an element with the class attribute value:

Find_element_by_class_name ("BG s_btn")

4. Tag positioning

Locate elements with tags such as <div>, <input>, <a>, and so on:

Find_element_by_tag_name ("input")

5. Link positioning

Specifically used to position text links to locate elements by linking the contents of text:

Find_element_by_link_text ("Baidu News")

6. Partial link positioning

A supplement to link positioning, some text links longer, this time to take part of the text information to locate the element (the condition is to uniquely identify the link)

Find_element_by_partial_link_text ("[Hundred] degrees new [smell]")

7. XPath positioning

XPath is a language for locating elements in an XML document, because an HTML page can be considered an implementation of XML. The XPath positioning method can be implemented up to 99%

Strategy:

(1) Absolute path positioning (not recommended, due to poor strain on the page)

(2) using element attribute positioning (any attribute of an element can be, as long as it is unique)

Find_element_by_xpath ("//input[@id = ' kw ']")

Find_element_by_xpath ("//*[@maxlength = ' 100 ']")

(3) combination of attributes and hierarchies

Find_element_by_xpath ("//span[@class = ' bg s_ipt_wr ']/input[2]")

(4) Using logical operators

Find_element_by_xpath ("//input[@id = ' kw ' and @class = ' su ']")

8. CSS Positioning

CSS is a language used to describe the performance of HTML and XML documents. CSS uses selectors to bind properties to page elements. In general, the positioning speed is faster than XPath, and positioning success rate of 99%

Authentication method: In the browser console, enter:$ (") Example: Enter $ ('. BG s_btn ') to verify that the element is positioned to

Positioning strategy:

(1) using the class attribute to locate:

Find_element_by_css_selector (". BG s_btn")

(2) Location by id attribute:

Find_element_by_css_selector ("#kw")

(3) Positioning by any attribute:

Find_element_by_css_selector ("[autocomplete= ' off ']")

(4) Combination positioning:

Find_element_by_css_selector ("span.bg s_btn>input#su")

Webdriver API (element positioning and manipulation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.