Selenium WebDriver CheckBox and Radio Button
Radio Button
Using the click() method a radio button can be accessed by clicking on it.
Using https://www.facebook.com/, see that radiobutton1.click() toggles on the “Female” radio button. Radiobutton2.click() toggles on the “Male” radio button leaving the “Female” unselected.
WebElement maleradiobutton2 = driver.findElement(By.id(“u_0_7”)); //Female Radio Button1 is selected //Female Radio Button1 is de-selected and Radio Button2 is selected Let’s create a test case in which we will automate the following scenarios to handle Radio buttons: Checking and Un-Checking a check box on/off is done using the click() method. The below code will click on Yahoo “Stay signed in” check box two times and display the output as TRUE when it is clicked on, and FALSE if it is Unchecked. Step 1: Go to the site https://login.yahoo.com/config/login? Step 2: Click on Sign in button Step 3: Right Click on Stay signed in and select Inspect Step 4: Inspect the element Stay signed in First Click– checkbox was clicked Second click – checkbox was unchecked The isSelected() method is used to know whether the Checkbox is selected on or off. Let’s create a test case in which we will automate the following scenarios to handle Checkbox: Now, we will create a test case step by step in order to understand of how to handle checkbox. Step 1: Launch the Eclipse IDE Step 2: Right click on the src folder and click on the New > class. Step 3: Enter the Class Name as (Checkbox) Step 4: Invoke the Google Chrome browser and set the system property to the path of your chromedriver.exe file. Step 5: Navigate to the “spicejet” website. Step 6: Now we try to locate the “Armed Forces/ Military ID card/ Dependent ID card” checkbox by inspecting its HTML code. Note the id attribute of a “Armed Forces/ Military ID card/ Dependent ID card” checkbox. Step 7: Write the code to handle the “Forces/ Military ID card/ Dependent ID card” checkbox. Complete Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Radiobutton{
public static void main(String[] args) {
// set the system property of a chromedriver.exe file to the path.
System.setProperty("webdriver.chrome.driver"," D:\\Drivers\\geckodriver.exe ");
// Launch the Google Chrome browser.
WebDriver driver = new ChromeDriver();
driver.get("https://www.facebook.com/");
WebElement femaleradiobutton1 = driver.findElement(By.id(“u_0_6”));
WebElement maleradiobutton2 = driver.findElement(By.id(“u_0_7”));
//Radio Button1 is selected
femaleradiobutton1.click();
System.out.println("Female Radio Button is Selected");
//Female Radio Button1 is de-selected and Male Radio Button2 is selected
maleradiobutton2.click();
System.out.println("Male Radio Button is Selected");
}
driver.close();
}
Check Box
Here is the complete code for above scenario:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", “ D:\\Drivers\\geckodriver.exe ");
WebDriver driver = new ChromeDriver();
driver.get("https://www.spicejet.com/");
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", “D:\\Drivers\\geckodriver.exe ");
WebDriver driver = new ChromeDriver();
driver.get("https://www.spicejet.com/");
System.out.println(driver.findElement(By.id("ctl00_mainContent_chk_IndArm")).isSelected());
driver.findElement(By.id("ctl00_mainContent_chk_IndArm")).click();
System.out.println(driver.findElement(By.id("ctl00_mainContent_chk_IndArm")).isSelected());
driver.close();
}
}
We have used two methods in the above code:
One Comment