Selenium + Python automation Test unittest Framework Learning (iii) Webdriver element positioning (i)

Source: Internet
Author: User
Tags tag name xpath

1.Webdriver principle

Webdirver is a Web automation tool that provides a unified Webdriver interface to the browser, which is submitted by client, our test script, and the remote server browser responds to the request. Compared to the original selenium1 in the Selenium RC is more convenient, the browser operation more flexible.

2. Positioning

(1) Positioning of elements:

The positioning of the elements can be achieved by Id,name,class Name,tag Name,link_text,partial_link_text,css Selector,xpath, etc.

The syntax format is: find_element_by_xxxx ()

For example, Baidu's search box

If you want to locate the Baidu search box, you can

find_element_by_id ("kw") or Find_element_by_class_name ("s_pt") or Find_element_by_name ("WD") or Find_element_by_tag_ Name ("Input")

Or through XPath positioning: You can use the Firepath tool to locate:

Xpath:find_element_by_xpth (".//*[@id = ' kw ']")

If a text link can be link_name ("text") or text information longer can be used to locate Partial_link_text ("partial text") through some textual information

All of the above positioning is accurate premise is to ensure that the positioning element brackets filled with the uniqueness of the information can be accurately positioned on the element, such as positioning class_name, you must determine that only the element is applied to the CALSS name, otherwise please replace other positioning methods.

The positioning of the elements is generally recommended to have the ID, with the name, with the two positioning more accurate, XPath is an important way to locate.

*.xpath positioning

Although XPath positioning can be obtained using a tool like Firepath, we still have to be clear about the syntax of XPath positioning so that later we can change the code to see exactly which element is positioned.

Path expression for XPath:

Path Expression Results
Bookstore Selects all child nodes of the bookstore element.
/bookstore

Select the root element bookstore.

Note: If the path starts with a forward slash (/), this path always represents the absolute path to an element!

Bookstore/book Selects all book elements that belong to a child element of bookstore.
//Book Selects all book child elements, regardless of their position in the document .
Bookstore//Book Selects all book elements that belong to descendants of the bookstore element, regardless of where they are located under bookstore .
@Lang Select all attributes that are named Lang.

An expression with a predicate, which is called the bracketed description in the pragmatics, selects all that can be used *

Path Expression Results
/bookstore/book[1] Selects the first book element that belongs to a bookstore child element.
/bookstore/book[last ()] Select the last book element that belongs to the bookstore child element.
/bookstore/book[last ()-1] Select the second-to-last book element that belongs to the bookstore child element.
/bookstore/book[position () <3] Select the first two book element that belongs to the child element of the bookstore element.
title[@lang] Select all the title elements that have properties named Lang.
title[@lang = ' Eng '] Selects all title elements, and these elements have the lang attribute value of Eng.
/BOOKSTORE/BOOK[PRICE>35.00] Selects all the book elements of the bookstore element, and the value of the price element must be greater than 35.00.
/bookstore/book[price>35.00]/title Selects all the title elements of the book element in the bookstore element, and the value of the price element must be greater than 35.00.

(2) Positioning a group of objects

Locating a single element is find_element, then locating a group of objects is find_elements

Locating a group of objects is an element that requires bulk operations on a set of objects or that requires selecting multiple conditions or similar elements, which are the qualifying elements that are required to filter the final positioning of a set of objects and then traverse through the filter criteria.

For example: tick all checbox elements or do the same for a group of elements

Checbox =  find_elements_by_xx ()          #首先定位一组元素for in   checkbox:                       # Traverse Check all checkboxes    I.click ()

(3) Level positioning

Many elements do not have a canonical ID or name to locate, and the element's class name and tag name are the same, and it is the first layer of elements, it is difficult to locate, so the way to locate the element is to locate the parent element, and then set the seat-level elements

Example: Menu List sub-level menu label positioning

If you want to locate the news label, we can locate the UL element first, then locate the a element, so that the positioning is more accurate

Parent = find_element_by_id ("ul"= Parent.find_element_by_name ("  tj_tnews") Chidren.click ()

(4) Positioning objects in a frame

Some pages of the framework nested another frame, if you need to locate the nested frame of the content can be positioned to the external frame, and then positioned to the nested frame, and then you can locate the page elements inside, in fact, this idea and positioning level elements are the same, but here to use the positioning framework statement needs to write down

Switch_to_frame (ID)

For example, frame a nested this frame B, you now need to position the text box in frame B

Switch_to_frame (id == b) find_element_by_id ("textboxid")

After you have positioned the popup frame, you need to jump out of the frame to locate the elements on the original page.

Driver.switch_to_frame ("layui-layer-iframe1")           #定位框架 ... driver.switch_to_ Default_content ()                      jump out of frame Driver.find_element_by_xpath ("html/body")

Selenium + Python automation Test unittest Framework Learning (iii) Webdriver element positioning (i)

Related Article

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.