Joe
Joe

Reputation: 1

Selenium Webdriver Xpath: Find and compare the text of an element then click the link that's 4 rows after

I want to find and compare the heading in the table. Then click on the delete button that in the next 3 rows of the heading.

Example: Lets say a given input is Heading 2. I want to search Heading 2 and see if it exists then click on the delete button that is associated with 'Heading 2' (like 3-4 rows after).

So far this is the code that i came close to. The only problem is that it always click the first selection instead of where i want it to go.

String HeadingName = "Heading 2"
driver.findElement(By.xpath("//tr[contains(.,'" + HeadingName + "')]/tr[position()=4]/td[2]/div/a[2]")).click();

This is what the table and it's code looks like.

Heading 1
Name: Joe
Gender: Male
Options: Update | Delete
Heading 2
Name: Jenny
Gender: Female
Options: Update | Delete

 <table>
 <tbody>
  <tr>
       <th class="st-head-row" colspan="2">Heading 1</th>
  </tr>
  <tr class="even">
      <td class="st-key">Name:</td>
      <td class="st-val">Joe</td>
  </tr>
  <tr class="even">
      <td class="st-key">Gender:</td>
      <td class="st-val">Male</td>
  </tr>
  <tr class="even last-row">
      <td class="st-key">Options:</td>
      <td class="st-val">
           <div style="white-space:nowrap;">
                <a id="save" href="linkaddress">save</a>
                | 
                <a id="delete" href="linkaddress">delete</a>
           </div>
      </td>
  </tr>
  <tr>
       <th class="st-head-row" colspan="2">Heading 2</th>
  </tr>
  <tr class="even">
      <td class="st-key">Name:</td>
      <td class="st-val">Jenny</td>
  </tr>
  <tr class="even">
      <td class="st-key">Gender:</td>
      <td class="st-val">female</td>
  </tr>
  <tr class="even last-row">
      <td class="st-key">Options:</td>
      <td class="st-val">
           <div style="white-space:nowrap;">
                <a id="save" href="linkaddress">save</a>
                | 
                <a id="delete" href="linkaddress">delete</a>
           </div>
      </td>
  </tr>
 </tbody>
</table>

Upvotes: 0

Views: 3878

Answers (4)

Joe
Joe

Reputation: 1

I actually made an answer that surprisingly worked for me.

dUtil.findElement(By.xpath("//tr/th[contains(text(),'" + headingName + "')]/../following::tr[3]/td[2]/div/a[2]")).click();

I got every condition I wanted.

  1. Searches for the heading that is similar to the given input heading
  2. Clicks the link 'Delete' that is 3 rows bellow the searched element
  3. Should work even if there are more than one batch of rows (batch meaning from heading to delete)
  4. The batch doesn't have to be in order.

Upvotes: 0

Ian Roberts
Ian Roberts

Reputation: 122414

I'd try and keep the XPath a bit more "semantic", e.g.

//tr[th = 'Heading 2']/following-sibling::tr[starts-with(td[1], 'Options')][1]//a[. = 'delete']

Here I'm looking specifically for the "delete" link in the next "Options" row, rather than something more fragile like "the second link in the next-but-two row".

Upvotes: 0

Dmitry Shyshkin
Dmitry Shyshkin

Reputation: 384

Try next code with xpath

String headingName = "Heading 2";

driver.findElement(By.xpath("//th[text()='"+ headingName +"']/parent::tr/following-sibling::tr[@class='even last-row']/td[@class='st-val']/div/a[@id='delete']")).click();

If it works, for detailedinfo how this xpath is created look here - http://www.w3schools.com/xpath/xpath_axes.asp

Upvotes: 0

Juhi Saxena
Juhi Saxena

Reputation: 1217

Try this code , hope it will help you.

driver.findElement(By.xpath("//tr/th[contains(text(),'" + HeadingName + "')]//following::tr[3]/td[2]/div/a[2]")).click();

Thanks

Upvotes: 0

Related Questions