1

I have below HTML which is having a table with 15 rows (below are the 2 rows for example)-

<table id="task" style="width:100%" class="border">
<thead>
<tr>
<th>Task </th>
<th>Request date</th>
<th>Description</th>
<th>file</th>
<th>number</th>
<th>Request</th>
<th>Tel</th>
<th>Lan</th>
<th></th></tr></thead>
<tbody>
<tr class="odd">

<td>TASK21</td>
<td><label class="readonly">25/05/2018</label></td>
<td>file</td>
<td></td>
<td>
<a href="/taskId=1&amp;action=dispatchFromOneTaskType">123</a></td>
<td>1</td>
<td>23654</td>
<td>En</td>
<td><form id="tasksForm" method="post" action="tasks.do?action=delete" class="formulaire"><input type="hidden" name="language" value=""><input type="hidden" name="taskId" value="1"><input type="submit" value="Close"></form></td></tr>
<tr class="even">

<td>TASK45</td>
<td><label class="readonly">25/05/2018</label></td>
<td>file</td>
<td>321654</td>
<td>
<a href="s.do?taskId=&amp;action=dispatchFromOneTaskType">753536</a></td>
<td>1</td>
<td>123456</td>
<td>En</td>
<td><form id="tasksForm" method="post" action="s.do?action=delete" class="formulaire"><input type="hidden" name="language" value=""><input type="hidden" name="taskId" value="2"><input type="submit" value="Close"></form></td></tr>
</tbody>
</table>

I want to search any row's 1st column by giving TASK number (e.g- TASK42) and then traverse to last column (9th) of that row and then click on the Button.

To do so, I am using below Xpath-

//*[text()='TASK42'][@id='tasksForm']/input[3]

Since the desired TASK can be in any of the 15 rows of the table, I can't give Relative/Absolute Xpath. Hence first searching for that TASK in any row and then traversing to the last column of that row.

But it is not working. Can you please help?

1

//*[text()='TASK42'][@id='tasksForm']/input[3]

won't be working for your case. The syntax like this [some][condi][tion] is like AND logical conjunction. So in your query you're saying

I want to take all the elements (whatever the tag they represented with) which have the inner text TASK42 AND "id" attribute that is equal to tasksForm

Obviously the elements which have TASK in the inner text do not have attributes (in your example). The mentioned attribute belongs to the form element which is a child of TASK element. So, more relevant xPath would be:

//tr[td[text()='TASK21']]/td/form/input[3]

The xpath above is saying

I want to take tr (a table row) that contains td element (as a child) having the inner text TASK21. When found I want to get to the form element under that tr element (since found tr is common parent for the form we're searching and for the td element that has to contain given text) and then take third input from there

To make that work for your particular task you should build this xpath in the code like:

String xPath = "//tr[td[text()='TASK" + taskNum + "']]/td/form/input[3]";

Where taskNum is a variable that contains the task number.

0

I am writing one simple API which will return you the desired xpath of the button that is to be clicked.

// _task_id: TASK42 --> Should return row containing TASK42
private Integer get_row_num(String _task_id){
    String _xpath = "//table[@id = 'task']//tr/td" ;
    List<String> _all_tasks = new ArrayList<>();
    for(WebElement w : driver.findElements(By.xpath(_xpath))){
        _all_tasks.add(w.getText());
    }
    return _all_tasks.indexOf(_task_id);
}

/**
 * @param _task_id 
 * @return: xpath for the button which needs to be clicked
 */
public String build_xpath(String _task_id){
    int _row_num = get_row_num(_task_id);
    String _xpath = "//table[@id = 'task']/tr[" + _row_num + "]/td[9]";
    return _xpath;
}

I think the code is simple and easy to follow. You need to use the public method which takes the taskId as argument and returns the xpath of the button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.