Leo
Leo

Reputation: 189

selenium webdriver xpath generation

I want to create an xpath for clicking on "run " (4th column) based on the first column value (xyz). the below xpath doesnt work. Can you suggest a better way of writing the xpath.

//table/tbody/tr/td[text()='xyz fix']/parent::tr/td[4]
<div id="main">
   <table class="FixedLayout" width="1000px">
      <tbody>
         <tr></tr>
         <tr>
            <td class="RowHeight">
               <a href="/TestPass/View/373">xyz</a>
            </td>
            <td>xyz fix</td>
            <td>1125</td>
            <td>
               <a href="/Instance/Create?suiteId=373">Run</a>
            </td>
         </tr>
         <tr>
            <td class="RowHeight">
               <a href="/TestPass/View/372">abc</a>
            </td>
            <td>abc fix</td>
            <td>1125</td>
            <td>
               <a href="/Instance/Create?suiteId=372">Run</a>
            </td>
         </tr>
      </tbody>
   </table>
</div>

Upvotes: 0

Views: 3049

Answers (1)

Yi Zeng
Yi Zeng

Reputation: 32895

I don't see why your one didn't work. Please clarify what it means "doesn't work". NoSuchElementException? ElementNotVisibleException? Wrong XPath? Not clicking the link or what?

Meanwhile, try the following XPaths (but the issue could be your Selenium code instead of XPath):

Here I assume you want to the <a> link instead of <td>, because you mentioned you want to click it.

Use XPath predicate:

//*[@id='main']//table/tobdy/tr[td[text()='xyz']]/td[4]/a

Use XPath predicate with attribute selector to avoid using index.

//*[@id='main']//table/tobdy/tr[td[text()='xyz']]//a[contains(@href, 'Instance/Create')]

Use .. to get the parent

//*[@id='main']//table/tobdy/tr/td[text()='xyz']/../td[4]/a

Upvotes: 2

Related Questions