edward.eas
edward.eas

Reputation: 59

How to click a button from a table where all classes have the same name

<tbody>
  <tr class="b">
    <td class="t_l">betmatrix</td>
    <td class="t_l">
    <td class="t_r">dsdfsf</td>
    <td class="t_r">NONE</td>
    <td class="t_r">ALL</td>
    <td class="t_r">NONE</td>
    <td class="t_r">ALL</td>
    <td class="t_r">ALL</td>
    <td class="t_r">NONE</td>
    <td class="t_r">ALL</td>
    <td class="t_r">Not required</td>
    <td class="t_r">1.11</td>
    <td class="t_r">All User</td>
    <td class="t_r">0.00</td>
    <td class="t_r">2014-09-05</td>
    <td class="t_r">2014-09-15</td>
    <td class="t_r">2014-09-05</td>
    <td class="t_r">1-admin</td>
    <td class="t_r">
    <td class="t_r">0.00</td>
    <td class="t_r">0.00</td>
    <td class="t_r">
    <td class="t_r">
      <a onclick="showBonusModificationLogs(53599)" href="#">History</a>
    </td>
  </tr>
  <tr class="a">
  <tr class="b">
  <tr class="a">
</tbody>

This is the table structure that I have, and there is no way I can search only for specific items in the table.

How can I find an element in a row and then click on the button at the end of the row if it is found?

In this example I would need to search for the word: dsdfsf

It was easier for other tables because the classnames were different. But the same method will not work here.

Upvotes: 1

Views: 736

Answers (2)

SiKing
SiKing

Reputation: 10329

As @alecxe pointed out, you can use XPath. There are probably hundred different ways of doing this. But to match your requirements I would use: //tr[td[text()='dsdfsf']]/td/a. This says: any row that has a column with the text 'dsdfsf', locate a column with an anchor tag.

Upvotes: 3

alecxe
alecxe

Reputation: 473833

You can find element By.xpath:

WebElement element = driver.findElement(By.xpath('//table//tr[td[@class="t_r"] and text() = "dsdfsf"]/td[last()]'));
element.click();

The xpath would search in every tr inside the table for the td with class t_r and text dsdfsf, then would get the last td inside the row.

Upvotes: 2

Related Questions