3. Working on Checkboxes:
----------------------------------
We use click() command to set "ON/OFF" for a checkbox
isSelected() command used to check that checkbox is set ON/OFF, based on that
it will return true/false
Exp: Write script to select checkbox in "https://login.salesforce.com" login page
Script:
WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://login.salesforce.com");
driver.findElement(By.id("rememberUn")).click();
Exp: Write script to uncheck 2nd checkbox in "http://echoecho.com/htmlforms09.htm" page
Script:
WebDriver driver= new FirefoxDriver();
driver.get("http://echoecho.com/htmlforms09.htm");
driver.manage().window().maximize();
//to uncheck 2nd checkbox
driver.findElement(By.xpath("html/body/div[2]/table[9]/tbody/tr/td[4]/table/tbody/tr/td/
div/span/form/table[1]/tbody/tr/td/table/tbody/tr[2]/td[3]/input[2]")).click();
Exp: Write script to select the checkboxes that are unselected on "http://echoecho.com/htmlforms09.htm" page
Script:
WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.echoecho.com/htmlforms09.htm");
//to find number of checkboxes in a page
List chks= driver.findElements(By.name("Checkbox"));
System.out.println("number of checkboxes are :"+chks.size());
//to click on checkbox which are not selected
for(WebElement chk:chks){
if(chk.isSelected()==false){
chk.click();
}
}
4. Working on Radiobuttons:
-----------------------------------
We can use click() command to operate radiobutton
Exp: Write script to click on each radio button in facebook home page and to print name of each radiobutton
Script:
WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://facebook.com");
//to find number of radiobuttons
List radios=driver.findElements(By.className("_58mt"));
System.out.println("number of radiobuttons are :"+radios.size());
//to select radiobuttons one after one
for (WebElement radio: radios){
//to read radiobutton name
System.out.println(radio.getText());
radio.click();
Thread.sleep(3000);
}
----------------------------------
We use click() command to set "ON/OFF" for a checkbox
isSelected() command used to check that checkbox is set ON/OFF, based on that
it will return true/false
Exp: Write script to select checkbox in "https://login.salesforce.com" login page
Script:
WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://login.salesforce.com");
driver.findElement(By.id("rememberUn")).click();
Exp: Write script to uncheck 2nd checkbox in "http://echoecho.com/htmlforms09.htm" page
Script:
WebDriver driver= new FirefoxDriver();
driver.get("http://echoecho.com/htmlforms09.htm");
driver.manage().window().maximize();
//to uncheck 2nd checkbox
driver.findElement(By.xpath("html/body/div[2]/table[9]/tbody/tr/td[4]/table/tbody/tr/td/
div/span/form/table[1]/tbody/tr/td/table/tbody/tr[2]/td[3]/input[2]")).click();
Exp: Write script to select the checkboxes that are unselected on "http://echoecho.com/htmlforms09.htm" page
Script:
WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.echoecho.com/htmlforms09.htm");
//to find number of checkboxes in a page
List
System.out.println("number of checkboxes are :"+chks.size());
//to click on checkbox which are not selected
for(WebElement chk:chks){
if(chk.isSelected()==false){
chk.click();
}
}
4. Working on Radiobuttons:
-----------------------------------
We can use click() command to operate radiobutton
Exp: Write script to click on each radio button in facebook home page and to print name of each radiobutton
Script:
WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://facebook.com");
//to find number of radiobuttons
List
System.out.println("number of radiobuttons are :"+radios.size());
//to select radiobuttons one after one
for (WebElement radio: radios){
//to read radiobutton name
System.out.println(radio.getText());
radio.click();
Thread.sleep(3000);
}
No comments:
Post a Comment