[Selenium With C#基础教程] Lesson-07 复选框
作者:Surpassme
来源:http://www.jianshu.com/p/98ede43da3c3
声明:本文为原创文章,如需转载请在文章页面明显位置给出原文链接,谢谢。
复选框也是Web页面常见的一种用户交互控件,可以允许用户一次选择多个选项。常见的多选按钮示例如下图所示:

HTML源码如下:
请选择你常用的交通出行方式:<br /><br />
<input type="checkbox" name="metro" checked="checked" id="metro" />地铁
<input type="checkbox" name="bus" id="bus" />公交
<input type="checkbox" name="bike" id="bike" />自行车
<input type="checkbox" name="walk" id="walk" />步行
<input type="checkbox" name="driving" id="driving" />自驾
通过Name选中复选框
driver.FindElement(By.Name("metro")).Click();
通过ID选中复选框
对于复选框的点击而言实际第一次点击将选中,再次点击将清除选中状态(针对第一次状态是未选中状态而言),下面将演示在测试脚本中如何确认复选框是否为选中状态。
IWebElement metroCheckbox = driver.FindElement(By.Id("metro"));
if (!metroCheckbox.Selected)
{
metroCheckbox.Click();
}
通过连续调用FindElement方法选中复选框
针对要定位的复选框具有多个相同的属性时,我们可以使用XPath定位,也可以使用连续调用FindElement方法。HTML源码如下:
<div id="div1">
<input type="checkbox" name="test" checked="checked" value="on" />复选框位于第一个div里面
</div>
<div id="div2">
<input type="checkbox" name="test" checked="checked" value="on" />复选框位于第二个div里面
</div>
定位的脚本代码如下所示:
driver.FindElement(By.Id("div2")).FindElement(By.Name("test")).Click();
清除复选框选中状态
对于清除复选框的状态,我们只需要再次点击复选框即可,与单选按钮一样,不能使用Clear()方法。因为复选框的状态而言总共就两种状态,选中和未选中状态。
IWebElement drivingCheckbox = driver.FindElement(By.Id("driving"));
if (drivingCheckbox.Selected)
{
drivingCheckbox.Click();
}
else
{
drivingCheckbox.Click();
}
断言复选框的状态
判断复选框的方法比较简单,只需要使用Assert类中的方法即可。
IWebElement bikeCheckbox = driver.FindElement(By.Id("bike"));
Assert.IsFalse(bikeCheckbox.Selected);
bikeCheckbox.Click();
Assert.IsTrue(bikeCheckbox.Selected);
使用用户自定义插件的筛选框,如iCheck
在Web页面中开发人员经常会使用一些插件自定义的控件,如下图即是采用iCheck插件的自定义复选框:

以下为HTML部分源码:
<ul class="list">
<li>
<div class="icheckbox_flat-red checked"><input tabindex="13" id="flat-checkbox-1" style="position: absolute; opacity: 0;" type="checkbox">
<ins style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none;
opacity: 0;"></ins>
</div>
<label for="flat-checkbox-1" class="">Checkbox 1</label>
</li>
<li>
<div class="icheckbox_flat-red checked"><input tabindex="14" id="flat-checkbox-2" checked="" style="position: absolute; opacity: 0;" type="checkbox">
<ins style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none;
opacity: 0;"></ins>
</div>
<label for="flat-checkbox-2" class="">Checkbox 2</label>
</li>
</ul>
完整代码如下:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using System.Collections.ObjectModel;
using System.Threading;
using System.Drawing;
namespace SeleniumDemo
{
[TestClass]
public class Lesson07
{
IWebDriver driver = null;
[ClassInitialize]
public static void TestInitialize(TestContext context)
{
Console.WriteLine("初始化TestInitialize");
}
[TestInitialize]
public void TestInitialize()
{
driver = new ChromeDriver();
Console.WriteLine("开始进行测试");
}
[TestMethod]
public void TestCheckbox()
{
Console.WriteLine("调用测试方法");
string url = "http://www.bootcss.com/p/icheck/";
driver.Navigate().GoToUrl(url);
driver.Manage().Window.Maximize();
IWebElement checkBox1Ele = driver.FindElement(By.XPath("//ul[@class='list']/li[1]/div[contains(@class,'icheckbox_flat-red')]"));
IWebElement checkBox2Ele = driver.FindElement(By.XPath("//ul[@class='list']/li[2]/div[contains(@class,'icheckbox_flat-red')]"));
int position = checkBox1Ele.Location.Y;
((IJavaScriptExecutor)driver).ExecuteScript("$(window.scrollTo(0," + position + "));");
checkBox1Ele.Click();
checkBox2Ele.Click();
Assert.IsTrue((driver.FindElement(By.XPath("//ul[@class='list']/li[1]/div[contains(@class,'icheckbox_flat-red')]/input"))).Selected);
Assert.IsFalse((driver.FindElement(By.XPath("//ul[@class='list']/li[2]/div[contains(@class,'icheckbox_flat-red')]/input"))).Selected);
}
[TestCleanup]
public void TestCleanup()
{
Console.WriteLine("结束测试");
driver.Quit();
}
[ClassCleanup]
public static void ClassCleanup()
{
Console.WriteLine("清理ClassCleanup");
}
}
}
在上面完整的代码中,点击Checkbox按钮和判断Checkbox的状态,所使使用的元素不一样,不知各位大神能否帮忙解释一下原因?其实不管是什么类型的Web控件,定位的基本方法都是Webdriver API提供的8种基本方法,在日常测试中灵活加以应用,可大大提高日常工作效率。[作者:Surpassme]
[Selenium With C#基础教程] Lesson-07 复选框的更多相关文章
- 自动化测试基础篇--Selenium单选框(Radio)复选框(CheckBox)
摘自:https://www.cnblogs.com/sanzangTst/p/7686602.html 一.什么是单选框.复选框? 二.单选框:radio 三.复选框:checkbox 四.判断是否 ...
- 转:jQuery LigerUI 使用教程表格篇(3) 复选框、多表头、分组、汇总和明细
阅读目录 复选框 多表头 分组 汇总 明细 复选框 grid可以设置复选框模式进行多选,只需要简单的配置 checked:true 获取选中行 如果要获取选中的行,可以用getSelecteds方法: ...
- 吾八哥学Selenium(三):操作复选框checkbox/单选框radio的方法
复选框checkbox和单选框radio是web网站里经常会使用到的两个控件,那么在web自动化测试的时候如何利用Selenium来操作这俩控件呢?今天我们就来简单入门练习一下! html测试页面代码 ...
- selenium 操作复选框
场景 从上一节的例子中可以看出,webdriver可以很方便的使用findElement方法来定位某个特定的对象,不过有时候我们却需要定位一组对象, 这时候就需要使用findElements方法. 定 ...
- 自动化测试-15.selenium单选框与复选框状态判断
本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...
- selenium+Python(定位 单选、复选框,多层定位)
1.定位一组元素webdriver 可以很方便的使用 findElement 方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用 findElements 方法.定位一组对象 ...
- 【selenium+Python WebDriver API】之复选框顺序正选和顺序反选
from selenium import webdriver from selenium.webdriver.common.by import By import os,time driver = w ...
- Selenium WebDriver-操作复选框
#encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...
- python selenium单/复选框操作
一.单选:radio 1.首先是定位选择框的位置 2.定位id,点击图标就可以了,代码如下(获取url地址方法:把上面源码粘贴到文本保存为.html后缀后用浏览器打开,在浏览器url地址栏复制出地址就 ...
随机推荐
- JavaScript的4种this调用模式
方法调用模式:函数调用模式:构造器调用模式:apply调用模式: 方法调用模式: 当一个函数被保存为对象的一个属性时,我们称它为一个方法.当一个方法被调用时,this被绑定到该对象. 函数调用模式: ...
- JWPlayer使用指南
http://support.jwplayer.com/customer/portal/articles/1499103-quickstart-reference <div id="m ...
- JavaScript字符集编码与解码
一.字符集 1)字符与字节(Character) 字符是各种文字和符号的总称,包括乱码:一个字符对应1~n个字节,一字节对应8位,每位用0或1表示. 2)字符集(Character Set) 字符集是 ...
- HDU 1006 [Tick Tick]时钟问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1006 题目大意:钟表有时.分.秒3根指针.当任意两根指针间夹角大于等于n°时,就说他们是happy的, ...
- 技巧收集-W1701
2017.02 For Flask, to use the decorator, apply it as innermost decorator to a view function. When ap ...
- NMEA-0183协议解析
NMEA-0183 NMEA 0183是美国国家海洋电子协会(National Marine Electronics Association )为海用电子设备制定的标准格式.目前业已成了GPS导航设备 ...
- Salesforce的Auto Number
在Salesforce中新建Object的时候,可以对Name选择Auto Number,即自动编号.如果没有仔细阅读说明的话,会有一个很容易让人迷惑的地方. 在选择时候,Salesforce提供的示 ...
- C++ cout 输出小数点后指定位数
在C中我们可以使用 printf("%.2lf",a);但在C++中是没有格式操作符的,该如何操作: C++使用setprecision()函数,同时必须包含头文件iomanip, ...
- shiro的入门实例-shiro于spring的整合
shiro是一款java安全框架.简单而且可以满足实际的工作需要 第一步.导入maven依赖 <!-- shiro --> <dependency> <groupId&g ...
- ConnectString ()函数的介绍
ConnectString ()函数的介绍: connectstring 函数主要负责数据库的连接工作 Public Function ConnectString() As String ...