Handle checkbox and radio button using JavascriptExecutor

Category: Selenium WebDriver 159 0

We have already discussed the technique to handle checkbox and radio button in Selenium WebDriver. The technique which we have discussed earlier is simple and default one. As we have also discussed that sometimes click button command does not work in Selenium, hence, in such case, JavascriptExecutor technique to click on web element came as the savior.

Today we are going to discuss the scenario which occurs when checkbox become disabled and radio button is not clickable. Thus, in this case, also JavascriptExecutor is the savior.

Revision from the earlier tutorial on checkbox and radio button handling

I would highly recommend you to first read the default tutorial on checkbox and radio button handling. If those techniques don’t work for you then try this tutorial. Just for the purpose of revision here are some of the points that we already discussed in the earlier tutorial which is about checkbox and radio button handling:

  • HTML tag for radio button: <input type=’radio’ name=’some_name’ class=’some_class’ />
  • HTML tag for checkbox button: <input type=’checkbox’ name=’some_name’ class=’some_class’ />
  • Methods to perform actions on the checkbox and radio button are-
  1. isSelected();
  2. isDisplayed();
  3. isEnabled();

So these are the three important points we had discussed earlier. Let’s move ahead with this tutorial.

checkbox and radio button in Selenium

Click on radio button which is disabled using JavascriptExecutor

Sometimes radio button appears disabled due to some environmental issues, hence, in such situation this scenario helps us to resolve the roadblock. We use JavascriptExecutor interface to handle such situation and here is the sample command for the same:

 

This JavascriptExecutor fetches the DOM and find an element by its id and click on the element. Here is the entire program:

Explanations

In this program, we are opening the homepage of Facebook and clicking on the Female radio button. However, in this case, everything looks normal for the Facebook radio button.

checkbox button selenium

Select any unchecked checkbox button using JavascriptExecutor

Sometimes the status of checkbox button comes as false so in such case, normal click method doesn’t work. Therefore, we handle such scenario by using JavascriptExecutor. Here is the sample command to handle such situation in Automation testing.

 

Let’s jump to the program to click on the checkbox with JavascriptExecutor:

Explanations

In this program, we are login in to the memotome website. This site is used to create various memo and reminders. Then WebDriver logins to the website and further it selects the checkbox button which is attached to the test memo.

Hope this tutorial will resolve the roadblocks by clicking on the radio as well as checkbox button. If you still have any doubts then feel free to write in comments below. Meanwhile, I have an exclusive post for you:

 

Related Articles

Add Comment