4.8 selenium 定位一组元素

定位一组元素的方法:
(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()
定位一组元素与一个元素的区别在于,前者是elements,后者是element

selenium示例有两个文件:checkbox.htmlcheckbox.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" href="https://github.com/bootcdn/BootCDN/blob/1.0.1/ajax/libs/bootstrap/3.3.0/css/bootstrap-theme.min.css">
    <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 自动化测试 定位一组元素-->
    <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>

效果:选取一组元素,从组中选取自己要定位的复选框

#checkbox.py
from selenium import webdriver
import os,time

#selenium自动化测试 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()
定位一组元素与一个元素的区别在于,前者是elements,后者是element
'''


warning :未经授权,不得转载
有问题的小伙伴请在下方留言,喜欢就点个赞吧;关注我,带你一起写bug
CSDN:带只拖鞋去流浪
知乎:带只拖鞋去流浪
哔哩哔哩:带只拖鞋去流浪

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。