首页 > 代码库 > selenium测试(Java)-- 一组元素操作(十一)

selenium测试(Java)-- 一组元素操作(十一)

利用下面的例子来编写测试脚本

技术分享

页面代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>CheckBox</title>
<link
    href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"
    rel="stylesheet" />
<script
    src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script>
</head>
<body>
    <h3>checkbox</h3>
    <div class="well">
        <form class="form-horizontal">
            <div class="control-group">
                <label class="control-lable" for="c1">checkbox1</label>
                <div class="controls">
                    <input type="checkbox" id="c1" />
                </div>
            </div>
            <div class="control-group">
                <label class="control-lable" for="c2">checkbox2</label>
                <div class="controls">
                    <input type="checkbox" id="c2" />
                </div>
            </div>
            <div class="control-group">
                <label class="control-lable" for="c3">checkbox3</label>
                <div class="controls">
                    <input type="checkbox" id="c3" />
                </div>
            </div>
        </form>
    </div>
</body>
</html>

脚本:

package com.test.findelement;

import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CheckBox {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        WebDriver driver = new FirefoxDriver();
        driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/findelement/checkbox.html");
        driver.manage().window().maximize();
        
        List<WebElement> WebElements = driver.findElements(By.cssSelector("[type=checkbox]"));
        
        Iterator iterator = WebElements.iterator();
        
        //全部勾选
        while(iterator.hasNext()){
            WebElement wb = (WebElement)iterator.next();
            wb.click();
        }
        //去掉第一个
        waitTime(3000);
        driver.findElements(By.cssSelector("[type=checkbox]")).get(0).click();
        
        //去掉最后一个
        waitTime(3000);
        driver.findElements(By.cssSelector("[type=checkbox]")).get(WebElements.size()-1).click();
        
        
        waitTime(5000);
        driver.quit();
    }
    
    static public void waitTime(int time) {

        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 

selenium测试(Java)-- 一组元素操作(十一)