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>

     

  • 相关阅读:
    C++ Primer 笔记——标准库类型string
    Windows文件系统
    c++数组
    B+树
    简单搭建FastDFS分布式文件系统(简单易懂)
    什么是分布式系统(通俗易懂)
    对List中每个对象元素按时间顺序排序
    java23种设计模式之一: 策略模式
    微信app支付java后台流程、原理分析及nei网穿透
    quartz多任务调度+spring 实现
  • 原文地址:https://www.cnblogs.com/yan-xiang/p/6617633.html
Copyright © 2011-2022 走看看