Select radio button:
Let’s create code which will check, on the web page, if the radio button is present or not and after making sure that the radio button is present and is not selected, we will click/select the radio button.
This code will check :
This code will check :
if the radio button is displayed/present or not ,
if the radio button is enabled or not
if the radio button is selected or not
To check displayed or not, we use the predefined method
isDisplayed()
To check enabled or not, we use the predefined method
isEnabled()
To check selected or not, we use the predefined method
isSelected()
And to click the radio button , we use click()
Let’s open facebook sign up page on chrome browser and click the radio button
for the Female.
Click the radio button for the Female. |
Below is the main method:
public static void main(String[] args) throws Exception {
System. setProperty("webdriver.chrome.driver", "path_of_chrome_driver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
//maximize the chrome
browser
driver.manage().window().maximize();
//enter the URL
driver.get("https://www.facebook.com/");
//locate the radio button for
the Female with its xpath.
WebElement radioButtonFemale = driver.findElement(By.xpath("//input[@type='radio'
and @value='1']"));
//checking if the radio button
is displayed, Boolean will return true or false.
boolean rb_displayed = radioButtonFemale.isDisplayed();
System.out.println("Did radio
button displayed ? "+rb_displayed);
//checking if the radio button
is enabled.
boolean rb_enabled = radioButtonFemale.isEnabled();
System.out.println("Radio
Button enabled ? "+rb_enabled);
//checking if the radio button
is selected or not.
boolean rb_selected = radioButtonFemale.isSelected();
System.out.println("Radio
Button already selected ? "+rb_selected);
// Let’s make a condition, if
the radio button already selected, then do noting.
if (rb_selected){
System.out.println("Radio
Button already selected = "+ rb_selected);
}
//If the radio button is not
already selected, then select it.
if (!rb_selected){
radioButtonFemale.click(); // clicks the
radio button
//Now to check
if the radio button selected or not
rb_selected = radioButtonFemale.isSelected();
System.out.println("Radio
button clicked/selected ? "+rb_selected);
}
driver.close();
}
Output:
Did radio button displayed ? true
Radio Button enabled ? true
Radio Button already selected ? false
Radio button clicked/selected ? true
No comments:
Post a Comment