Home > Back-end >  How do I check if a radio button is clicked or not in selenium?
How do I check if a radio button is clicked or not in selenium?

Time:12-01

So I am trying to check if element1 radio button is clicked then if it is clicked I want to click element2 radio button, but if element2 radio button clicked then I want element1 to be clicked I tried this code and it nots showing any errors or anything but it does give the result I want

wait.until(ExpectedConditions.elementToBeClickable(object.element1));
wait.until(ExpectedConditions.elementToBeClickable(object.element2));

if(object.element1.isSelected()){
object.element2.click();
}
if(object.element2.isSelected()){
object.element1.click();
}

enter image description here

I tried the code I add mentioned above and it is not what the expected result was, instead it just stays the same

CodePudding user response:

In your code, if radio button 1 is selected already, then the first if condition is true, so it executes the code, result - radio button 2 is selected. Then while checking the 2nd if condition - radio button 2 is selected now, this is also true, so this condition also executes, result - radio button 1 is selected.

So, you have to change the if condition:

if(object.element1.isSelected()){
    object.element2.click();
} else {
    object.element1.click();
}
  • Related