Quality Testing

Quality is delighting customers

How to verify radio button selected or not in Selenium WebDriver ?

<div class="col-2">
<div class="radio">
<label>
<input class="ng-valid ng-touched ng-dirty ng-valid-parse" type="radio" ng-model="dealCtrl.auctionType.value" ng-value="dealCtrl.auctionType.options.firstPrice.value" name="auctionTypeOptions" ng-change="dealCtrl.auctionType.onChange()" value="1">
<span class="label ng-binding">First Price</span>
</label>
<label>
<input class="ng-valid ng-dirty ng-touched" type="radio" ng-model="dealCtrl.auctionType.value" ng-value="dealCtrl.auctionType.options.secondPrice.value" name="auctionTypeOptions" ng-change="dealCtrl.auctionType.onChange()" value="2">
<span class="label ng-binding">Second Price</span>
</label>
</div>

Can any one please share the code. How to verify radio button selected or not in Selenium WebDriver ?

Views: 3880

Reply to This

Replies to This Discussion

Hi,
You can verify in 2 ways:

1. Directly you can use .isSelected()
eg System.out.println(driver.findElement(By.xpath("")).isSelected());
Here take the xpath or any locator for 1st radio button. This will give True or False as per the radio button is selected or not

2.
Use a common method
public static void radiobtn(WebElement element){
boolean btn = element.isSelected();
if(btn == true){
System.out.println("Radio button is Selected:"+element.getAttribute("checked"));
}else{
System.out.println("Radio button is Selected:"+element.getAttribute("checked"));
}
}
In the main script just call the method
Here it will check the boolean value as per the selected or not and enter into the loop.

IsSelected() - will work

boolean CheckboxDefaultStatus = driver.findelement(By.xpath("//span[contains(text(),'First Price')]").isSelected();

if(CheckboxDefaultStatus ==true)

{

System.out.println("First Price checkbox got selected" +"\n" +"\n");

}

else

{

System.out.println("First Price checkbox not selected" +"\n" +"\n");

}

Selenium webdriver is a very popular tool among software testing companies to automate web applications.

To work on the radio buttons first we have to locate and then we can perform specific actions on the Radion Buttons using Selenium WebDriver.

You can find the radio element using any locator provided by selenium.

Like for e.g driver.findElement(By.id("Radio"));

Selenium webdriver provides few methods which we can use for pre & post-validate the radio button states.

Methods:

isSelected(): this method checks if the button is selected or not.
isDisplayed(): it checks if the radio button is displayed or not.
isEnabled(): it checks if the radio button is enabled for interaction or not.


We can use isSelected() method and check if the element is not selected and then we can use this code below to perform click only if the radio button is not selected.

WebElement radio = driver.findElement(By.id("radio-1"));
boolean radioState = radio.isSelected();

//performs click if radio button is not selected
if(radioState == false) {
radio.click();
}

Please do let me know if you have any doubts. 

RSS

TTWT Magazine


Advertisement

Advertisement

Advertisement

Advertisement

© 2021   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service