1. Hierarchical positioning (secondary positioning)
In the actual test process, a page may have multiple elements with basically the same attributes. If you want to locate one of them, you need to use hierarchical positioning at this time. Position its parent element first, and then position the element through the parent element.
Example: Positioning the Sogou input box by hierarchy
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(r'https://www.sogou.com/')
form_element = driver.find_element_by_id('sf') # 获取form表单元素
form_element.find_element_by_id('query').send_keys('selenium') # 通过表单定位输入框
form_element.find_element_by_id('stb').click() # 通过表单定位搜索按钮
time.sleep(3)
driver.quit()
As in the above code, we first locate the form form, and then locate the input box and button below through the form.
Note: The above example only locates the element through two levels, and the level positioning does not necessarily need to be located twice, we can locate multiple times.
2. Locating a set of elements
And when we need to obtain multiple objects with the same attributes and need to operate the objects in batches, we will use find_elements to locate a group of elements.
Create the following html file named checkbox.html
<form class="form-horizontal">
<div class="control-group">
<label class="controllabel" for="China">中国人</label>
<div class="controls">
<input type="checkbox" id="China"/>
</div>
</div>
<div class="control-group">
<label class="controllabel" for="American">美国人</label>
<div class="controls">
<input type="checkbox" id="American"/>
</div>
</div>
<div class="control-group">
<label class="controllabel" for="German">德国人</label>
<div class="controls">
<input type="checkbox" id="German"/>
</div>
</div>
<div class="button">
<input type="submit" id="submit"/>
</div>
</form>
Example: select all the above multi-selection boxes
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(r'E:\xxx\checkbox.html') # 打开checkbox.html文件,使用绝对地址
checkboxs = driver.find_elements_by_xpath('//input[@type="checkbox"]') # 获取批量的对象
for checkbox in checkboxs: # 循环控制
if not checkbox.is_selected(): # 判断多选框是否被选中
checkbox.click() # 单击
time.sleep(3)
driver.quit()
3. Comprehensive application
When we need to locate a group of elements, there will be many similar elements on the page, so we need to use it together with the hierarchy. First locate the parent element of the group of elements, and then locate its descendants through the parent element.
Example 1: Obtain the content of search hot words on Sogou WeChat page
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(r'http://weixin.sogou.com/')
topele = driver.find_element_by_id('topwords') # 搜索热词的父元素
tops = topele.find_elements_by_tag_name('a') # 二次批量定位热词元素
for top in tops: # 循环获取元素
print(top.text) # 打印文本内容
driver.quit()
It is difficult to locate tables in UI automation testing. How to quickly obtain table data, please see the following examples.
Example 2: Locate the table to get the header
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(r'http://www.w3school.com.cn/cssref/css_selectors.asp')
# //table[@class="dataintable"]//tr[1]//th 获取表头元素
table_header = driver.find_elements_by_xpath('//table[@class="dataintable"]//tr[1]//th')
for header in table_header: # 循环获取元素
print(header.text) # 打印文本内容
driver.quit()
Example 3: Locate the data content of the second column of the table
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(r'http://www.w3school.com.cn/cssref/css_selectors.asp')
# //table[@class="dataintable"]//tr[y]//td[x] y第几条记录,x第几列数据
# //table[@class="dataintable"]//tr//td[2] 获取第二列数据
table_header = driver.find_elements_by_xpath('//table[@class="dataintable"]//tr//td[2]')
for header in table_header: # 循环获取元素
print(header.text) # 打印文本内容
driver.quit()
Example 4: Get all the data in the table
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(r'http://www.w3school.com.cn/cssref/css_selectors.asp')
# //table[@class="dataintable"]//tr 定位所有行
tables = driver.find_elements_by_xpath('//table[@class="dataintable"]//tr')
for tr in tables: # 循环每行元素
for td in tr.find_elements_by_tag_name('td'): # 循环获取列
print(td.text, end='\t\t')
print('\n')
driver.quit()
Locate the table, use the find_elements group to locate, use xpath=//table//tr[y]//td[x] (the record of y, the column of data in x), and locate when one of y or x has no value row or column. The following is the supporting information. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!
Software testing interview applet
The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!
The following interview question sections are covered:
1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux
6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics
method of obtaining: