Charles Ramsell
Charles Ramsell

Reputation: 45

Locate element using relative xpath and text descriptor

I have a test scenario where I create a new entry (Banana) into a table, then subsequently modify the entry, and then delete it.

The table has a modify button and a delete button next to the text description of the new entry. I would like to use relative xpath to locate the textual value, then somehow select the delete or modify button depending on the test scenario being executed. enter image description here

A sample of code being used is:

*** Settings ***
[Documentation]  Delete Fruit from table
Suite Teardown  Close all browsers
Library  Selenium2Library
Library  XvfbRobot
Library  Collections

*** Variables ***
${delFruit}  Banana

*** Test Cases ***
Delete Fruit Button
    wait until element is visible  xpath=//div[@id='${delFruit}']
    click element  xpath=//div[@id='${delFruit}']/a[2]/span
    confirm action

Here is a snip of the html behind the scenes - all of the delete buttons use the textual descriptor of "Delete Fruit":

 <a class="button micro primary error" onclick="deleteFruit(3)" href="javascript:void(0)">
    <span class="fa fa-trash" title="Delete Fruit" border="0" align="absmiddle"></span>
    </a>
    </td>
    <td class="cell ">Banana</td>

The issue is that when I create a new entry to the table, the table contents are modified alphabetically. so in actuality, the Banana entry is between the Apple and the Orange.

I could hard code the click element action to the delete button: click element xpath=/html/body/div[1]/div/div[2]/div/div[2]/table/tbody/tr[1]/td[1]/a[2]

I was hoping to find a way to identify the element to the left of the Banana xpath=/html/body/div[1]/div/div[2]/div/div[2]/table/tbody/tr[1]/td[1]/a[2] as the placement of the table items shifts as new items are added.

Does anyone have any suggestions on how to select the delete button to the left of the Banana?

Upvotes: 0

Views: 1822

Answers (1)

outcatcher
outcatcher

Reputation: 71

As far as understand your situation, prescending-sibling xpath axis should help:

//td[contains(text(), 'Banana')]/prescending-sibling::td/a[/span[contains(@title, 'Delete')]]

Upvotes: 2

Related Questions