In addition, we know that WebDriver has 8 methods to locate a single element. It is also mentioned that WebDriver also provides 8 corresponding positioning methods to locate a group of elements.
8 kinds of find_elements_by_* The method is as follows:
- find_elements_by_id: locate a group of elements through the ID attribute value of the element;
- find_elements_by_name: locate a group of elements through the name attribute value of the element;
- find_elements_by_class_name: locate a group of elements through the class attribute value of the element;
- find_elements_by_xpath: locate a set of elements through Xpath;
- find_elements_by_tag_name: locate a group of elements by their tag name;
- find_elements_by_css_selector: locate a set of elements through CSS selector;
- find_elements_by_link_text: locate a group of elements through the text information between element label pairs;
- find_elements_by_partial_link_text: locate a group of elements by partial text information between element label pairs.
1. Locate a group of element cases by tag name
Chapter 5 introduces that a set of elements can be located by tag name. The checkbox is simply written in html language html file. checkbox. The html code of the html page is as follows
<html> <head> <title>Check box test instance</title> </head> <body> Please choose your favorite fruit</br> <input type="checkbox" name="fruit" value ="apple" >Apple<br> <input type="checkbox" name="fruit" value ="orange">Orange<br> <input type="checkbox" name="fruit" value ="mango">Mango<br> </body> </html>
checkbox. The rendering effect of HTML page is shown in the figure.
checkbox.html page effect
Via checkbox The html code of the html page can see that the labels of the three check boxes are labels.
Case requirements:
Selenium program is designed through find_ elements_ by_ tag_ The name () method implementation selects the check boxes of three kinds of fruits.
The code is as follows:
#Method 1: find_elements_by_tag_name from selenium import webdriver from time import sleep driver = webdriver.Firefox() driver.get("file:///D:/checkbox. HTML ") #checkbox.html path should be adjusted according to your actual situation inputs = driver.find_elements_by_tag_name("input") for i in inputs: # By looking at the source code, you can use either type or name, because the three elements and the two attributes of the three fruits are the same if i.get_attribute("type") == "checkbox": # if i.get_attribute("name") == "fruit": i.click() sleep(3) driver.quit()
Through the above code, you can see that it is through find_ elements_ by_ tag_ The name method locates all elements labeled.
You can also use find_ elements_ by_ The XPath () method implements the operations required by this case. The code is as follows:
#Method 2: find_elements_by_xpath from selenium import webdriver from time import sleep driver = webdriver.Firefox() driver.get("file:///D:/checkbox.html") checkboxes = driver.find_elements_by_xpath("//input[@name='fruit']") print(len(checkboxes)) for checkbox in checkboxes: checkbox.click() driver.quit()
2. Hierarchical positioning
As mentioned above, hierarchical alleles may be applied when the following situations occur.
If the element defined as cannot uniquely identify itself through its own attributes, you can consider positioning yourself with the help of superior elements. For example, a baby is born with no name and ID number. When checking a baby, it is often labeled as "the daughter of a certain woman". Because the mother of the baby is certain, finding the mother will find the baby. This is the case with the combination of XPath hierarchy and attribute positioning.
Hierarchical positioning can also be used to locate a group of elements. For example, common tables, drop-down selection boxes, etc. Hierarchical positioning may be used.
The case is shown in the figure:
- Obtain the value of the cell [Guangdong] in the table through hierarchical positioning;
- Select Shanghai from the [native place] drop-down list.
Effect drawing of pattern example
Case html source code# Learning objectives:
<html> <head> <title>form&Drop down selection box</title> </head> <style> .box{ width:500px;height:800px; margin:20px auto; text-align:center; } </style> <body> <table border="1" id="qw" align="center"> <tr> <th>province</th> <th>city</th> </tr> <tr> <td>Guangdong</td> <td>Zhejiang</td> </tr> <tr> <td>Guangzhou</td> <td>Hangzhou</td> </tr> </table> </br> <div class="box"> <label>Native place</label> <select name="site"> <option value="0">Beijing</option> <option value="1">Shanghai</option> <option value="2">Shenzhen</option> </select> </div> </body> </html>
Through hierarchical positioning, the code is as follows:
from selenium import webdriver driver=webdriver.Firefox() driver.get("file:///D:/ TableSelect.html") table=driver.find_element_by_id("qw") #Get number of rows row=table.find_elements_by_tag_name("tr") #Get the number of columns col=row[0].find_elements_by_tag_name("th") #Get the first row and the first column Row_Col=row[1].find_elements_by_tag_name("td")[0].text print(Row_Col) # Select the second value from the drop-down box checkvalue=driver.find_element_by_name("site") checkvalue.find_element_by_xpath("//option[@value='1']").click()