Sapna
Sapna

Reputation: 411

Can you suggest a good xpath for the below scenario?

My html code is as below -

<table id="tblResults_" class="jtable ui-widget-content">
<thead>
<tbody>
<tr class="jtable-data-row jtable-row-even jtable-row-selected" data-record-key="-1" style="">
<td class="jtable-command-column">
<td class="jtable-command-column">
<td class="jtable-command-column">
<td>-1</td>
<td>** new record **</td>
<td/>
<td/>
<td/>
<td/>
<td/>
<td/>
<td/>
<td/>
<td/>
<td class="jtable-command-column">
<button class="jtable-command-button jtable-delete-command-button" title="Delete" style="background-color: transparent;">
</td>
</tr>
</tbody>
</table>

Scenario to automate :- In a dynamically generated table , i need to add a new data row .Then i need to verify the added data and delete it .

Currently i folllow the following steps :- 1)verify the row in which data is added . 2)With reference to the row number , click on the delete button with an xpath as below.

xpath=//*[@id='tblResults_']/tbody/tr["+rownumber+"/td[15/button

This xpath is working . But as per the current scenario i need to make the td also flexible.

So i went for the below xpath:-

driver.findElement(By.xpath("//*[@id='tblResults_']/tbody/tr/*/button[contains(@title,"Delete")]"));

But when i am paste the above code to eclipse it is giving me some error which says left side should be a variable .

Can anyone help me here ?

Requirements - Row number to be variable . column number should not be considered . button class or title can be considered .

Upvotes: 1

Views: 106

Answers (1)

alecxe
alecxe

Reputation: 474141

You just need to handle quotes properly:

"//*[@id='tblResults_']/tbody/tr/*/button[contains(@title, 'Delete')]"

Also, if this is always the last tr that you need to check:

"//*[@id='tblResults_']/tbody/tr[last()]//button[contains(@title, 'Delete')]"

Upvotes: 2

Related Questions