zoukankan      html  css  js  c++  java
  • webdriver(python)学习笔记四——定位一组元素

    webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用find_elements方法。

    定位一组对象一般用于以下场景:

    • 批量操作对象,比如将页面上所有的checkbox都勾上
    • 先获取一组对象,再在这组对象中过滤出需要具体定位的一些对象。比如定位出页面上所有的checkbox,然后选择最后一个

    checkbox.html

    <html>
            <head>
                <meta http-equiv="content-type" content="text/html;charset=utf-8" />
                <title>Checkbox</title>
                <script type="text/javascript" async="" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
                <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
                <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
            </head>
            <body>
                <h3>checkbox</h3>
                <div class="well">
                    <form class="form-horizontal">
                        <div class="control-group">
                            <label class="control-label" for="c1">checkbox1</label>
                            <div class="controls">
                                <input type="checkbox" id="c1" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="c2">checkbox2</label>
                            <div class="controls">
                                <input type="checkbox" id="c2" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="c3">checkbox3</label>
                            <div class="controls">
                                <input type="checkbox" id="c3" />
                            </div>
                        </div>                      
                        <div class="control-group">
                            <label class="control-label" for="r">radio</label>
                            <div class="controls">
                                <input type="radio" id="r" />
                            </div>
                        </div>                      
                    </form>
                </div>
            </body>
        </html>

    此脚本需要与自动化脚本同一目录下。

    将html打开显示如下图所示:

    可看出共打开三个复选框,一个单选框。

    方法一:定位三个复选框

    # coding:utf-8
    
    from selenium import webdriver
    import time
    import os
    
    dr=webdriver.Firefox()
    file_path='file:///'+os.path.abspath('checkbox.html')
    
    dr.get(file_path)
    
    #选择页面上的所有的input,然后从中选出所有的checkbox并勾选上
    
    inputs=dr.find_elements_by_tag_name('input')
    for input in inputs:
        if input.get_attribute('type')=='checkbox':
            input.click()
    time.sleep(1)
    
    dr.quit()

    这里input的attribute类型有checkbox,也有radio,也可以将其设为radio,则可将其选中。

    第二种方法:

    # coding:utf-8
    
    from selenium import webdriver
    from time import sleep
    import os
    
    dr=webdriver.Firefox()
    file_path='file:///'+os.path.abspath('checkbox.html')
    dr.get(file_path)
    
    #选择页面上的所有的checkbox并选中
    
    checkboxes=dr.find_elements_by_css_selector('input[type=checkbox]')
    for checkbox in checkboxes:
        checkbox.click()
    sleep(1)
    
    #打印当前页面上共有多少个checkbox
    print len(dr.find_elements_by_css_selector('input[type=checkbox]'))
    sleep(1)
    
    
    dr.quit()
        

    两种方式都是通过循环来选中元素,两种方法定位时不同,一个是name,一个是css方式。

    上面两种方法都是将同一类型的复选框全部选中的方法,那如何只取其中的几个呢。实现方式可以是先全部选中,再去掉某个。

    如何去掉勾选:

    把最后一个选中复选框去掉方法。

    # coding:utf-8
    
    from selenium import webdriver
    from time import sleep
    import os
    
    dr=webdriver.Firefox()
    file_path='file:///'+os.path.abspath('checkbox.html')
    dr.get(file_path)
    
    #选择页面上的所有的checkbox并选中
    
    checkboxes=dr.find_elements_by_css_selector('input[type=checkbox]')
    for checkbox in checkboxes:
        checkbox.click()
    
    
    #打印当前页面上共有多少个checkbox
    print len(dr.find_elements_by_css_selector('input[type=checkbox]'))
    
    #将页面上最后1个checkbox的勾选运去掉
    
    dr.find_elements_by_css_selector('input[type=checkbox]').pop().click()
    sleep(2)
    
    dr.quit()

    去掉勾选的逻辑就是先选中,然后再次点击取消选中。在此使用的方法的pop()方法即删除最后一个元素。

  • 相关阅读:
    Beacon技术是什么?
    exclude kernel or other packages from getting updated
    (OK) running CORE & docker on Fedora 23 server
    (OK) dnf
    (OK) dnf
    rpm
    dnf
    dnf install -y kernel-4.2.3-300.fc23
    paper4—Multi-MPE_Trigger_Algorithm—testing
    paper4—Multi-MPE_Trigger_Algorithm
  • 原文地址:https://www.cnblogs.com/kongzhongqijing/p/3533779.html
Copyright © 2011-2022 走看看