2

I am working on a project where I have to locate a button. I have used XPath for that, but the button's id changes on every refresh, so I am facing a problem with that. Below is a HTML excerpt. Let me know how I can identify that button so that it will not cause an error even if the id changes

<tr>
  <td/>
  <td>
    <div id="LoginButton" style="float: left;">
      <table style="width: auto;" id="ext-comp-1032" class="x-btn x-btn-noicon x-btn-over x-btn-focus" cellspacing="0">
        <tbody class="x-btn-small x-btn-icon-small-left">
          <tr>
            <td class="x-btn-tl">
              <i>&nbsp;</i>
            </td>
            <td class="x-btn-tc"/>
            <td class="x-btn-tr">
              <i>&nbsp;</i>
            </td>
          </tr>
          <tr>
            <td class="x-btn-ml">
              <i>&nbsp;</i>
            </td>
            <td class="x-btn-mc">
              <em class="" unselectable="on">
                <button class=" x-btn-text" id="ext-gen12" type="button">Login</button>
              </em>
            </td>
            <td class="x-btn-mr">
              <i>&nbsp;</i>
            </td>
          </tr>
          <tr>
            <td class="x-btn-bl">
              <i>&nbsp;</i>
            </td>
            <td class="x-btn-bc"/>
            <td class="x-btn-br">
              <i>&nbsp;</i>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </td>
</tr>
2
  • You had the question itself in the formatted code block. Separated the question from the sample for you. Commented Feb 15, 2012 at 6:17
  • 1
    You need to provide the full page HTML if you want somebody to give you an accurate XPATH. Without knowing the structure of the full page and which id's are static and which are dynamic it is very hard to give an accurate answer. Generally speaking work your way up the dom to a static ID and start your XPATH there.
    – Ardesco
    Commented Feb 15, 2012 at 12:30

5 Answers 5

1

If you always have 2 of them (like ext-gen11 and ext-gen12 in your sample), this will work too:

//button[text()="Login"][1] for ext-gen11
//button[text()="Login"][2] for ext-gen12

But depending on other (more reliable) elements, might be a better idea.
As suggested earlier:

//div[@id="LoginButton"]/../../../button[text()="Login"] for ext-gen11
//div[@id="LoginButton"]//button[text()="Login"] for ext-gen12

2
  • 1
    I would avoid using text()="Login" if at all possible, this allows you to run your automation in localized builds as well. It is generally a best practice to use identifiers in the markup rather than identify them by text visible to the user.
    – Sam Woods
    Commented Feb 16, 2012 at 17:14
  • @SamWoods, True. Commented Feb 18, 2012 at 4:52
3

If there is an element above the button that is uniquely identifiable you could search only in that scope, assuming that the button is the only button in that scope. You could also try to partially match the button id (not sure if the entire thing changes, or just part of it) or match the button's class if it does not change. If there is no way to identify a unique parent or ancestor (I wouldn't know since you didn't provide the entire html), you could first find the <div id="LoginButton"> and then go back up from there 3 levels and then find the button under that. That XPath would look like this:

//div[@id="LoginButton"]/../../../button
1
  • i have tried solution //button[text()='Login'] and its working :)
    – rohit ware
    Commented Feb 17, 2012 at 5:44
2

If you're already using XPath for this, can you start your XPath earlier on, for example, from the table that it appears as though this is in.

1
  • still i am not able to locate that button other than id//*[@id="ext-gen11"]
    – rohit ware
    Commented Feb 15, 2012 at 6:52
0

You can try getting all button values into an array and then select whichever you need. In Selenium API we have getAllButtons() method through which we can get all button's ids. Store this into an array as

String allbuttonIds [] = selenium.getAllButtons();

And then access the button whichever you want by index.

0

Try to use more than one identification value. This should work.

Or record the script using IDE and see what it picks up. Use the value IDE picks up. This also works.

But be careful. If you are running your script in IE and the IDE has picked up the CSS path, then you may get some problem.

Your Answer

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

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