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">
<th>Task </th>
<th>Request date</th>
<tr class="odd">

<td><label class="readonly">25/05/2018</label></td>
<a href="/taskId=1&amp;action=dispatchFromOneTaskType">123</a></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><label class="readonly">25/05/2018</label></td>
<a href="s.do?taskId=&amp;action=dispatchFromOneTaskType">753536</a></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>

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-


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?



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:


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.


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))){
    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.