In this session you will learn how to handle checkboxes and radio buttons on a webpage and also learn how to get number of radiobuttons/checboxes from webpages and also print them for your testing.
To get all selenium programs with example, please download the project from below github repository
https://github.com/knowledgeshare-tec...
Checkbox Overview
Check boxes on a Web Page is selected using click() Method
To check if a checkbox is selected or not isSelected() Method is used
Radio button Overview
Radio buttons on a Web Page is selected using click() Method
To check if a checkbox is selected or not isSelected() Method is used
General Tests performed on Checkboxes and Radio Buttons
Selecting - click()
DeSelect - click()
Is it Enabled - isEnabled()
Is it Selected - isSelected()
How many Radio buttons / Checkboxes present on a webpage – Store in a ListLTWebElementGT
Print all Radio buttons /Checkboxes available on a webpage - Store in a ListLTWebElementGT
==============================
Radio button program
==============================
package com.seleniumbasics;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class radio_buttons_example {
public static void main(String args[])
{
System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.navigate().to("http://demowebshop.tricentis.com/regi...");
WebElement male_select=driver.findElement(By.xpath("//input[@id='gender-male']"));
male_select.click();
if(male_select.isSelected())
{
System.out.println("male radio button is selected");
}
else
{
System.out.println("male radio button is not selected");
}
List<WebElement> radio_buttons=driver.findElements(By.xpath("//input[@type='radio']"));
//Getting number of radio buttons
System.out.println("Number of radio buttons on a web page : " +radio_buttons.size() );
//Printing the radio buttons
for(WebElement printradio: radio_buttons)
{
System.out.println(printradio.getAttribute("id"));
}
}
}
=================
checkbox program
=================
package com.seleniumbasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class checkbox_example
{
public static void main(String args[])
{
System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.navigate().to("http://the-internet.herokuapp.com/che...");
WebElement checkbox_1=driver.findElement(By.xpath("(//input[@type='checkbox'])[1]"));
checkbox_1.click();
WebElement checkbox_2=driver.findElement(By.xpath("(//input[@type='checkbox'])[2]"));
if(checkbox_2.isSelected())
{
System.out.println("checkbox2 is checked already and unchecking it.....");
checkbox_2.click();
}
}
}
コメント