zoukankan      html  css  js  c++  java
  • python selenum ---如何定位一组元素

    使用findElements方法定位一组对象

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

    · 批量操作对象,比如将页面上所有的checkbox都勾上

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

    复制代码
    <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="r1" />
                        </div>
                    </div>
                    
                    <div class="control-group">
                        <label class="control-label" for="r">radio</label>
                        <div class="controls">
                            <input type="radio" id="r2" />
                        </div>
                    </div>
                </form>
            </div>
        </body>
    </html>
    复制代码

    将这段代码保存复制到记事本中,将保存成checkbox.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(2)
    
    dr.quit()
    复制代码

    你可以试着把input.get_attribute('type') == 'checkbox' 中的checkbox 变成radio ,那这个脚本定位的会是两个单选框。

     

    第二种定位方法:

    复制代码
    # -*- 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)
    
    # 选择所有的checkbox并全部勾上
    checkboxes = dr.find_elements_by_css_selector('input[type=checkbox]')
    for checkbox in checkboxes:
        checkbox.click()
    time.sleep(2)
    
    # 打印当前页面上有多少个checkbox
    print len(dr.find_elements_by_css_selector('input[type=checkbox]'))
    time.sleep(2)
    
    dr.quit()
    复制代码

    第一种用的name ,第二种用的CSS 。

     

     如何去掉勾选:

    还有一个问题,有时候我们并不想勾选页面的所有的复选框(checkbox),可以通过下面办法把最后一个被勾选的框去掉。如下:

    复制代码
    # -*- 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)
    
    # 选择所有的checkbox并全部勾上
    checkboxes = dr.find_elements_by_css_selector('input[type=checkbox]')
    for checkbox in checkboxes:
        checkbox.click()
    time.sleep(2)
    
    # 把页面上最后1个checkbox的勾给去掉
    dr.find_elements_by_css_selector('input[type=checkbox]').pop().click()
    time.sleep(2)
    
    dr.quit()
    复制代码

    其实,去掉勾选表也逻辑也非常简单,就是再次点击勾选的按钮。可能我们比较迷惑的是如何找到“最后一个”按钮。pop() 可以实现这个功能。

    好吧!在web自动化的学习过程中,我们必须要知道一些前端的东西,这里扩展一下:

    http://www.w3school.com.cn/js/jsref_pop.asp

  • 相关阅读:
    【微信公众号开发】【8】网页授权获取用户基本信息(OAuth 2.0)
    【微信公众号开发】【7】获取用户信息
    改革春风吹满地(多边形面积)
    You can Solve a Geometry Problem too(线段求交)
    Choose the best route(最短路)dijk
    A + B Problem II(大数加法)
    Graph(Floyd)
    Palindromes
    Wolf and Rabbit
    献给杭电五十周年校庆的礼物
  • 原文地址:https://www.cnblogs.com/saryli/p/5182738.html
Copyright © 2011-2022 走看看