0

I am passing in the checkbox ID, grabbing the element and then if the checkbox is not checked I want to check it. It wouldn't check the box though, so I was debugging and then it worked when I added a second one in, so I added 2 to make it work and cleaned up some clutter I had created with commented code and such, then I ran it again and it didn't check the checkbox on either click. The funny thing is that I am getting focus on the checkbox.

Could this be an issue when there are certain security measures in place for the code? Is there a workaround for this?

Not sure if it matters but I am using XUnit for the testing framework.

[Then(@"I check the '(.*)' checkbox")]
public void ThenICheckTheCheckbox(string checkBoxId)
{
    var checkBox = Ie.FindElementById(checkBoxId);
    var isChecked = checkBox.Selected;

    if (!isChecked)
    {
        checkBox.Click(); 
        checkBox.Click();
        isChecked = checkBox.Selected;

    }
    Assert.True(isChecked, $"the checkbox with id of: {checkBoxId}, is not checked");

}

The HTML for the checkbox is this

<input class="check-box" data-val="true" data-val-required="The Is Active field is required." 
    id="IsActive" name="IsActive" type="checkbox" value="true">
  • wouldn't let me add a new tag xunit – Malachi Jun 21 '16 at 16:55
  • I'd recommend rolling back to the untidy, working version then go from there. – Bookeater Jun 21 '16 at 20:28
  • @Bookeater my current solution has me executing a client side script against the page to check the checkbox – Malachi Jun 22 '16 at 12:12
  • Can you show me the checkbox Id you are using to find the element – user1519137 Jul 13 '17 at 9:38
  • pretty sure it is the ID in the HTML of the Element --> IsActive @user1519137 it has been over a year, I would have to find this code again. – Malachi Jul 13 '17 at 13:29
2

Rather than accessing the element by Id try doing it with xpath. I can see the there is an tag attached to this element. So you could make use of that.So for example if your checkbox is setup like this:

<div id = "divContainer">
<input class="check-box" data-val="true" data-val-required="The Is Active 
 field is required." 
id="IsActive" name="IsActive" type="checkbox" value="true">
 </div>

Then you can use something like this:

 bool selected 
 =driver.findElemeny(By.xpath(.//[@id='divContainer']/input)).isSelected();

And then you can make use of the selected variable to click or unclick the checkbox. If you could provide me with the full page's code then I will be able to write the full xpath for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.