定位一組元素的方法:
(1)find_elements_by_id()
(2)find_elements_by_name()
(3)find_elements_by_class_name()
(4)find_elements_by_tag_name()
(5)find_elements_by_link_text()
(6)find_elements_by_partial_link_text()
(7)find_elements_by_xpath()
(8)find_elements_by_css_selector()
定位一組元素與一個(gè)元素的區(qū)別在于,前者是elements,后者是element
selenium示例有兩個(gè)文件:checkbox.html朵耕,checkbox.py
checkbox.html中使用的bootstrap框架蟀悦,下面是checkbox.html效果圖
<!--checkbox.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" >
<script src="https://github.com/bootcdn/BootCDN/blob/1.0.1/ajax/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<style>
.control-label,.controls{
float: left;
}
</style>
</head>
<body>
<!--selenium 自動(dòng)化測試 定位一組元素-->
<h3>checkbox</h3>
<div class="well">
<from class="form-horizontal">
<div class="control-group">
<lable class="control-label" for="c1">checkbox1</lable>
<div class="controls"><input type="checkbox" id="c1"></div>
</div><br>
<div class="control-group">
<lable class="control-label" for="c2">checkbox2</lable>
<div class="controls"><input type="checkbox" id="c2"></div>
</div><br>
<div class="control-group">
<lable class="control-label" for="c3">checkbox3</lable>
<div class="controls"><input type="checkbox" id="c3"></div>
</div><br>
</from>
</div>
</body>
</html>
效果:選取一組元素低飒,從組中選取自己要定位的復(fù)選框
#checkbox.py
from selenium import webdriver
import os,time
#selenium自動(dòng)化測試 4.8 定位一組元素
class CheckBox():
def setup_method(self):
self.driver = webdriver.Chrome()
self.file_path = 'file:///' + os.path.abspath('checkbox.html')
self.driver.get(self.file_path)
def teardown_method(self):
self.driver.quit()
def check_box(self):
#選取頁面上所有的 tag name 為 input 的元素
self.inputs = self.driver.find_elements_by_tag_name('input')
#然后從從過濾出type為checkbox的元素霹崎,單擊勾選
for i in self.inputs:
if i.get_attribute('type') == 'checkbox':
i.click()
time.sleep(1) #休息一秒
if __name__ == '__main__':
checkBox = CheckBox()
checkBox.setup_method()
checkBox.check_box()
checkBox.teardown_method()
'''
定位一組元素的方法:
(1)find_elements_by_id()
(2)find_elements_by_name()
(3)find_elements_by_class_name()
(4)find_elements_by_tag_name()
(5)find_elements_by_link_text()
(6)find_elements_by_partial_link_text()
(7)find_elements_by_xpath()
(8)find_elements_by_css_selector()
定位一組元素與一個(gè)元素的區(qū)別在于胯陋,前者是elements蕾各,后者是element
'''
warning :未經(jīng)授權(quán)扒磁,不得轉(zhuǎn)載
有問題的小伙伴請?jiān)谙路搅粞裕矚g就點(diǎn)個(gè)贊吧式曲;關(guān)注我妨托,帶你一起寫bug
CSDN:帶只拖鞋去流浪
知乎:帶只拖鞋去流浪
嗶哩嗶哩:帶只拖鞋去流浪