I am in a weird situation where I have multiple rows with similar values in my rows and I need to select element which has link with text "Protractor" in first cell and text "NewAge" in second cell of same row.
<table width="100%" border="0" cellspacing="0" cellpadding="2" class="resultsTable"><tr><td>
<a href = "Mytitle.aspx">Protractor</a>
<td>NewAge</td>
</tr>
<tr><td>
<a href = "NewMytitle.aspx"> NewProtractor </a>
<td>NewAge</td>
</tr>
<tr><td>
<a href = "NewProtTile.aspx">Protractor</a>
<td>NewAge World</td>
</tr></table>
Now I want my hyperlink element to be picked based on both text values in first and second cell which is 'Protractor' and 'NewAge'
I want to use exact text search instead of containing text as many other rows has similar values
This is the xpath returned by Chrome.
//*[@id="PageContent"]/table/tbody/tr[2]/td/table/tbody/tr[3]/td[1]/a
I tried using xpath to select the link with text as 'Protractor' in first cell and "NewAge" in second cell of same row but not getting an desired results.
element(by.xpath("//tr['Protractor']['NewAge']"))
Can someone please help?
To identify/retrieve the
hyperlink element
based on both text values in first and second cell which is Protractor and NewAge you can use the followingxpath
:Here is how I would handle it. First I would get a list of all rows
The I would get a list of rows from that list where the first cell text equals
Protractor
Then from this list I would check the second cell equals
NewAge
If you throw all these together and log the count and text of secondMatches, the number of elements should only be one and the text should be
Protractor NewAge
letting you know you have the right row.You can use XPath to do this.
This starts with the table, finds an A that contains "Protractor", and then finds the next TD that contains "NewAge".
If it were me, I would wrap this in a function that takes in the two parameters to make it reusable. Then you can pass in any two strings, and it will return the value or click the element, whatever you want...
Try below xpath to find the exact row, it can match the only row in my chrome.
Above xpath is stricted by limited on first and second cell, if you don't care which cell, you can use below xpath:
Then find your wanted element inside the matched row.
For example, you want to click the link inside the fist cell:
Or you want to click a link which text is 'Delete' and you don't care it in which cell: