zoukankan      html  css  js  c++  java
  • Selenium-测试对象操作之:复选框checkbox

    复选框操作包括:选中、取消选中、全选

     

    案例:

    Python+Selenium代码

    # -*- coding: utf-8 -*-
    from selenium import webdriver
    import os
    import time

    file_path = os.path.abspath('checkbox.html')
    print file_path
    driver = webdriver.Chrome()
    driver.get(file_path)
    #选中一个复选框
    driver.find_element_by_id("c1").click()
    #打印该复选框的选中状态
    print driver.find_element_by_id("c1").is_selected()
    time.sleep(3)
    #取消选中
    driver.find_element_by_id("c1").click()
    print driver.find_element_by_id("c1").is_selected()

    # 全选:选择页面上所有的 tag name 为 input 的元素
    inputs = driver.find_elements_by_tag_name('input')
    #然后从中过滤出 tpye 为 checkbox 的元素,单击勾选
    for inp in inputs:
      if inp.get_attribute('type') == 'checkbox':
        inp.click()

    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>
    <input type="text" id="in1" value="干扰输入框" />
    </form>
    </div>
    </body>
    </html>

     

  • 相关阅读:
    AppleScript
    iOS 架构之文件结构
    Swift
    ERROR ITMS-90032: "Invalid Image Path
    ios中微信原生登陆的坑,ShareSDK的坑
    ios中OC给js传值的方法
    mac电脑中xcode怎么恢复还原快捷键设置
    ios 中 数组、字典转成json格式上传到后台,遇到的问题
    ios 中长按图片或者二维码,保存图片到手机的方法
    ios 中 Plus屏幕适配的问题,xib创建的cell在 Plus出现被拉大的情况
  • 原文地址:https://www.cnblogs.com/yan-xiang/p/6617633.html
Copyright © 2011-2022 走看看