Use the following example to write a test script
Page code:
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8" />5 <title>CheckBox</title>6 <Link7 href= "Http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"8 rel= "stylesheet" />9 <ScriptTen src= "Http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></Script> One </Head> A <Body> - <H3>CheckBox</H3> - <Divclass= "Well"> the <formclass= "Form-horizontal"> - <Divclass= "Control-group"> - <labelclass= "Control-lable" for= "C1">CheckBox1</label> - <Divclass= "Controls"> + <inputtype= "checkbox"ID= "C1" /> - </Div> + </Div> A <Divclass= "Control-group"> at <labelclass= "Control-lable" for= "C2">CheckBox2</label> - <Divclass= "Controls"> - <inputtype= "checkbox"ID= "C2" /> - </Div> - </Div> - <Divclass= "Control-group"> in <labelclass= "Control-lable" for= "C3">Checkbox3</label> - <Divclass= "Controls"> to <inputtype= "checkbox"ID= "C3" /> + </Div> - </Div> the </form> * </Div> $ </Body>Panax Notoginseng </HTML>
Script:
1 Package com.test.findelement;2 3 import Java.util.Iterator;4 import java.util.List;5 6 import org.openqa.selenium.By;7 import Org.openqa.selenium.WebDriver;8 import org.openqa.selenium.WebElement;9 import Org.openqa.selenium.firefox.FirefoxDriver;Ten One Public class CheckBox { A - Public static void Main (string[] args) { - //TODO auto-generated method stub the - 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]")); + A Iterator Iterator = Webelements.iterator (); at - //Check all - While (Iterator.hasnext ()) { - webelement wb = (webelement) iterator.next (); - Wb.click (); - } in //Get rid of the first - WaitTime (+); to driver.findelements (By.cssselector ("[Type=checkbox]")). Get (0). Click (); + - //Remove the last one the WaitTime (+); * driver.findelements (By.cssselector ("[Type=checkbox]")). Get (Webelements.size ()-1). Click (); $ Panax Notoginseng - WaitTime (the); the driver.quit (); + } A the static public void waitTime (int time) { + - try { $ Thread.Sleep (time); $ } catch (Interruptedexception e) { - //TODO auto-generated catch block - e.printstacktrace (); the } - }Wuyi the}
Selenium Test (Java)--a set of element operations (11)