WebDriver of Java+selenium locates page elements (2)

Selenium-Webdriver provides a powerful element positioning method that supports the following three methods: single object positioning method, multiple object positioning method and hierarchical positioning

1. Locate a single element

1  // Applicable to the case where the attribute of the element contains id, it is recommended to use 
2 WebElement we = drive.findElement(By.id("id" ));
 3  // Applicable to the case that the attribute of the element contains name, it is recommended to use 
4 WebElement we = drive.findElement(By.name("name" ));
 5  // General positioning method, suitable for poorly positioned elements, recommended 
6 WebElement we = drive.findElement(By.xpath("xpathExpression" )) ;
 7  // Applicable to link class elements, namely <a> tags, elements containing herf attribute 
8 WebElement we = drive.findElement(By.linkText("linkText" ));
 9  // Applicable to link class elements, Fuzzy match, i.e. element 
10 containing herf attribute WebElement we = drive.findElement(By.partialLinkText("linkText"));
11  // Applicable to the case where the attribute of the element contains class, the method of processing composite class (classname with spaces in the middle), the processing method only selects one value in the class attribute 
12 WebElement we = drive.findElement(By.className(" className" ));
 13  // General positioning method, suitable for poorly positioned elements 
14 WebElement we = drive.findElement(By.cssSelector("selector" ));
 15  // General positioning method, used for hierarchical positioning 
16 WebElement we = drive.findElement(By.tagName("tagName"));

2. Locate multiple elements

The findElements() method can return a list of elements that meet the conditions 

1  // Define a collection, locate elements with the same class at one time 
2 List<WebElement> elements = driver.findElements(By.className("className" ));
 3  // Operate through the subscript of the collection, the collection The first subscript is 0, and the parameter index is the subscript 
4 elements.get(index).click();

3. Hierarchical positioning

  The idea of ​​hierarchical positioning is to locate the parent element first, and then precisely locate the child elements we need to select from the parent element. The general application scenario of hierarchical positioning is that it is impossible to directly locate the element to be selected, but its parent element is relatively easy to locate. By locating the parent element and then traversing its child elements to select the desired target element, or you need to locate all child elements under an element . A typical application is the positioning of tables.

1  // Get the table element object    
2 WebElement table = driver.findElement(By.id("id" ));   
 3  // Get all the row objects in the table table, and get the row object to be queried.   
4 List<WebElement> rows = table.findElements(By.tagName("tr" )); 
 5  // Locate drop-down list 
6 WebElement select = driver.findElement(By.id("id" )); 
 7  // From drop -down Find all options in a list 
8 List<WebElement> option = a.findElements(By.tagName("option"));

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325890956&siteId=291194637