Use the following example to write a test script
Page code:
<! DOCTYPE html>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>class= "Well" > <formclass= "Form-horizontal" > <divclass= "Control-group" > <labelclass= "Control-lable" for= "C1" >checkbox1</label> <divclass= "Controls" > <input type= "checkbox" id= "C1"/> </div> </div > <divclass= "Control-group" > <labelclass= "Control-lable" for= "C2" >checkbox2</label> <divclass= "Controls" > <input type= "checkbox" id= "C2"/> </div> </div > <divclass= "Control-group" > <labelclass= "Control-lable" for= "C3" >checkbox3</label> <divclass= "Controls" > <input type= "checkbox" Id= "C3"/> </div> </div > </form> </div></body>Script:
Packagecom.test.findelement;ImportJava.util.Iterator;Importjava.util.List;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver; Public classCheckBox { Public Static voidMain (string[] args) {//TODO auto-generated Method StubWebdriver Driver=NewFirefoxdriver (); 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 (); //Check All while(Iterator.hasnext ()) {webelement WB=(webelement) iterator.next (); Wb.click (); } //Get rid of the firstWaitTime (3000); Driver.findelements (By.cssselector ("[Type=checkbox]"). Get (0). Click (); //remove the last oneWaitTime (3000); Driver.findelements (By.cssselector ("[Type=checkbox]"). Get (Webelements.size ()-1). Click (); WaitTime (5000); Driver.quit (); } Static Public voidWaitTime (intTime ) { Try{thread.sleep (time); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Selenium Test (Java)--a set of element operations (11)