2

I am attempting to automate tests on my company's product. They use a sort of a toggle. I have the HTML and an image of the toggle below. I've found a seeming answer at https://stackoverflow.com/a/24682401/901083, yielding XPath code of //div[@class='toggle btn btn-default off btn-sm'], but that div changes to "toggle btn btn-sm btn-primary" once clicked and "toggle btn btn-sm btn-default off" again when it comes back. How do I generically refer to that div no matter its setting?

HTML

<div style="width: 80px; height: 30px;" class="toggle btn btn-default off btn-sm" data-toggle="toggle">
    <input data-off="O" data-on="I" data-size="small" data-toggle="toggle" data-val="true" data-val-required="The OptInReceiveEmail field is required." 
        data-width="80px" id="OptInReceiveEmail" name="OptInReceiveEmail" tabindex="0" value="true" type="checkbox">
    <div class="toggle-group">
        <label class="btn btn-primary btn-sm toggle-on">I</label><label class="btn btn-default btn-sm active toggle-off">O</label>
        <span class="toggle-handle btn btn-default btn-sm"></span>
    </div>
</div>

Image
enter image description here

1
  • Sorry. I got moved over to another section of the project and didn't get a chance to try out the solution. I'll go at it again when I get to work on Monday. Commented Nov 8, 2015 at 22:23

1 Answer 1

3

Assuming there no other toggle buttons on the page, you can, for example, check for the presence of data-toggle attribute:

//div[@data-toggle]

If there are other toggle buttons and you need this particular "Email" toggle:

//div[@data-toggle][input/@id = "OptInReceiveEmail"]
Sign up to request clarification or add additional context in comments.

1 Comment

That works. For some reason, I thought that targeting the Div wasn't allowing for the click. I must have confused a different error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.