Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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>
share|improve this question
    
You had the question itself in the formatted code block. Separated the question from the sample for you. –  Lyndon Vrooman Feb 15 '12 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 Feb 15 '12 at 12:30

5 Answers 5

up vote 0 down vote accepted

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

share|improve this answer
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 Feb 16 '12 at 17:14
    
@SamWoods, True. –  Misha Akovantsev Feb 18 '12 at 4:52

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.

share|improve this answer
    
still i am not able to locate that button other than id//*[@id="ext-gen11"] –  rohit ware Feb 15 '12 at 6:52

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
share|improve this answer
    
i have tried solution //button[text()='Login'] and its working :) –  rohit ware Feb 17 '12 at 5:44

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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