Java+selenium webdriver Positioning the page element (ii)

Source: Internet
Author: User
Tags tagname

Selenium-webdriver provides a powerful method for locating elements, supporting the following three methods: positioning methods for individual objects, positioning methods and hierarchical positioning of multiple objects

1. Locating individual elements

1 //for the case where the attribute of the element contains an ID, it is recommended to use2Webelement we = drive.findelement (by.id ("id")));3 //for elements where the attribute contains name, it is recommended to use the4Webelement we = drive.findelement (By.name ("name"));5 //general positioning method, for the elements that are not suitable for positioning, recommended use6Webelement we = drive.findelement (By.xpath ("XPathExpression"));7 //elements that apply to linked classes, that is, <a> tags, elements that contain herf attributes8Webelement we = drive.findelement (By.linktext ("LinkText"));9 //applies to the element of the linked class, Fuzzy match, that is, the element containing the Herf attributeTenWebelement we = drive.findelement (By.partiallinktext ("LinkText")); One //for the case where the attribute of the element contains class, the method that handles the compound class (classname with spaces in the middle) is handled by selecting only one of the values in the class attribute AWebelement we = drive.findelement (By.classname ("ClassName")); - //Universal positioning method for poorly positioned elements -Webelement we = drive.findelement (By.cssselector ("selector")); the //Universal positioning method for hierarchical positioning -Webelement we = drive.findelement (By.tagname ("TagName"));

2. Positioning multiple elements

The Findelements () method can return an element that meets the criteria List

1 // defines a collection that positions the same class elements once 2 list<webelement> elements = driver.findelements (By.classname ("ClassName")); 3 // Operation by the subscript of the collection, the first subscript of the set is 0, the index is the subscript 4 elements.get (index). click ();

3. Level positioning

The idea of hierarchical positioning is to locate the parent element first and then pinpoint the child elements that we need to select from the parent element. The general scenario for hierarchical positioning is that it is not possible to navigate directly to the element that needs to be selected, but its parent element is easier to locate, either by locating the parent element and then traversing its child elements to select the desired target element, or by locating all the child elements under an element. A more typical application is the positioning of the table.

1 //Get Table Element Object2Webelement table = driver.findelement (by.id ("id")); 3 //get all the row objects in the table tables and get the row objects that you want to query. 4list<webelement> rows = table.findelements (By.tagname ("tr")); 5 //Locate drop-down list6Webelement select = Driver.findelement (by.id ("id")); 7 //Find all options from the drop-down list8list<webelement> option = a.findelements (by.tagname ("option"));

Java+selenium webdriver Positioning the page element (ii)

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.