Saturday, January 14, 2023

How to read the user selection from a radio button group using Selenium?

SeleniumUtil library has been occupying my free time for the past couple of months. Since I’ve already written at length about what it is I won’t do that again here but if you really want to know you look here [1] [2].

When I implemented the abstraction wrapper for the HTML radio button group I ran into a problem. How would someone go about reading the user selection form a radio button group? Take a look at this HTML block,



According to the HTML5 spec, Radio Buttons belonging to the same group would have the same name attribute value. So toggling a given radio button using Selenium is not too difficult, but unlike Checkbox's the Radio Buttons I've seen in the wild (including this W3C example) doesn't append a checked attribute upon user selection. This complicates things.

I first tried retrieving the selection using the $0 variable that stores the user selection. But it turned out it was a browser specific variable that is not exposed to JavaScript. So how can we get a handle on this element and read its value? I did this,


But you can save all the pain because this snippet and bunch of other JavaScript goodies are available in SeleniumUtil 0.7.5.  

No comments:

Post a Comment

What's in my Bag? EDC of a Tester