Check or Click In Selenium?

Selenium is a wonderful tool that allows scripting using a browser. Note that this is different from scripting in the browser. Essentially, it offers apis in multiple different languages (perl, java, ruby and python) to open a browser, and do all tasks that one could manually do such as filling in the fields, clicking links, submitting forms etc all done programatically.

One of the form controls is a radio button. Selenium offers apis to select a radio button. This function is called check where you can pass the element locator of the radio button to be selected. There is also a function called click to which one can pass a clickable element locator. My usecase was to select a radio button that’s not the default when the page was loaded. So, I simply used the check function and ran the selenium script but the result wasn’t as expected. It turned out that using click instead of the check function solved the problem. Can you take a guess why that was the case? Hint: It’s not a fault with Selenium.

The reason is that the radio button had some onclick javascript. The check api will not bother to generate a onclick event because, there is no click happening there. So, by using the click command, I managed to both change the radio button and also make the browser execute the javascript code that the application required to be executed.


Filed under Selenium

2 responses to “Check or Click In Selenium?

  1. Serik

    I think, that it is a problem of Selenium. I don’t think that use-case “check radio-button (or checkbox – there is the same problem with it) and don’t fire ‘onclick’ event” is needed for anyone. It is not situation from real life.

  2. A human can generate a check action without a click by selecting the user interface element with the keyboard. (tab-tab-tab-tab-tab-tab-tab-space.) People using alternative format browsers, and people who do not (or can not) use a mouse do this all the time. I do it when I’m dealing with large forms. 🙂

    If the javascript is only triggered onClick(), and not onChange(), it’s probably a bug.

    Chris – cej102937

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s