Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'd like to check if a radio button is selected or not with its id for instance.

Element that needs to be checked:

<input id="Evidence_of_Obstruction" class="" type="Radio" checked="" onclick="changeSaveStatus(this, "72");" value="0" name="Patency">
                This is the element that needs to be checked:  <input id="Evidence_of_Obstruction" class="" type="Radio" checked="" onclick="changeSaveStatus(this, "72");" value="0" name="Patency">
– user1398749
                May 16, 2012 at 13:52
                sorry guys can be with any Selenium command: verify, check whatever. I just need to check if they are selected or not. Thanks!
– user1398749
                May 16, 2012 at 13:54

Using Selenium IDE I prefer element ids, which would be close to what t3hn00b suggested:

Command: assertValue Target: id=Evidence_of_Obstruction Value: on

In WebDriver this would be something like:

IWebElement element = _driver.FindElement(By.Id("Evidence_of_Obstruction"));
if(element.Selected)
    ; //then it is selected
    ; //then it is NOT selected

Alternatively, as t3hn00b eluded to, you could check another attribute such as Value or Checked. This can be done like so:

IWebElement element = _driver.FindElement(By.Id("Evidence_of_Obstruction"));
string result = element.GetAttribute("checked"); // or replace checked with value
//then check the strings contence.

Please use this below code:-

driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
if(  exists = driver.findElements( By.id("your id") ).size() != 0)
    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

You can use rhis part of the code.

if   |   !selenium.isChecked("Patency")
   waitForElementPresent   |   name=Patency
   click                   |   name=Patency
   waitForValue            |   name=Patency   |   on
endIf

The code above will check to see if the box is checked. If not, then it will check the box and verify that the action occurred as expected. The only downfall to the method above is I haven't found a way to do this with a check box using anything except NAME or ID. This should work if you used the name of the check box, which in your case is I haven't gotten this to work using XPATH or CSS, which would help in the even that the box doesn't show a change in status [xpath] when you check/uncheck it.

In the case that your check box either doesn't have any change in code on the webpage or for some reason you can't use either ID or NAME, then you'll do something like this:

storeValue   |   xpath=//input[@id="Evidence_of_Obstruction"][@name="Patency"]   |   isBoxChecked
if           |   storedVars['isBoxChecked']=='on'
   goto   |   NEXT_TASK
   waitForElementPresent   |   xpath=//input[@id="Evidence_of_Obstruction"][@name="Patency"]
   click                   |   xpath=//input[@id="Evidence_of_Obstruction"][@name="Patency"]
   waitForValue            |   xpath=//input[@id="Evidence_of_Obstruction"][@name="Patency"]   |   on
endIf
label   |   NEXT_TASK

This code takes advantage of Selenium IDE identifying check boxes as unchecked (off) or checked (on). You can store the state of a check box and determine what to do with the check box, if anything, based on it's current state. Hopes this helps you or someone else who comes across this issue.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.