Radio Buttons too can be toggled on by using the click() method.
Using http://www.practiceselenium.com for practise, see that radio1.click() toggles on the "Option1" radio button. radio2.click() toggles on the "Option2" radio button leaving the "Option1" unselected.
Toggling a check box on/off is also done using the click() method.
The code below will click on Facebook's "Keep me logged in" check box twice and then output the result as TRUE when it is toggled on, and FALSE if it is toggled off.
isSelected() method is used to know whether the Checkbox is toggled on or off.
Here is another example: http://www.practiceselenium.com
Here is the complete working code
If you encounter NoSuchElementException() while finding elements, it means that the element is not found in the page at the point the Web driver accessed the page.
- Check your locator again using Firepath or Inspect Element in Chrome.
- Check whether the value you used in the code is different from the one for the element in Firepath now.
- Some properties are dynamic for few elements. In case, you find that the value is different and is changing dynamically, consider using By.xpath() or By.cssSelector() which are more reliable but complex ways.
- Sometimes, it could be a wait issue too i.e., the Web driver executed your code even before the page loaded completely, etc.
- Add a wait before findElement() using implicit or explicit waits.
- The table below summarizes the commands to access each type of element discussed above
|Check Box, Radio Button||click()||used to toggle the element on/off|