zoukankan      html  css  js  c++  java
  • Selenium学习笔记之007:定位一组元素

    一、场景

    webdriver可以很方便的使用findElement方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用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>


    其代码保存为html文件,打开其效果如下图:



    注:学习web自动化需要学习一些前端的知识,其链接为: http://www.w3school.com.cn/jsref/jsref_pop.asp


    二、定位方法

    第一种方法:

    通过浏览器打个这个页面我们看到三个复选框和两个单选框。下面我们就来定位这三个复选框。

    # -*- 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() 可以实现这个功能。

    尝试

    把find_elements_by_css_selector('input[type=checkbox]').pop().click() 中的checkbox 变成radio 会是什么效果?


    本文内容部分参考虫师webdriver文档。

  • 相关阅读:
    如何将CentOS的默认启动界面修改为图形界面or字符界面
    如何将CentOS的默认启动界面修改为图形界面or字符界面
    virtualbox下CentOS7安装增强功能
    蓝牙4.0
    HC-SR04超声波测距
    STM32F407 通用同步异步收发器(串口)
    STM32F4 TIM(外设定时器)
    STM32F4 系统定时器
    STM32F4 异常与中断
    LED和按键实验
  • 原文地址:https://www.cnblogs.com/haixianglan/p/13942938.html
Copyright © 2011-2022 走看看