Selenium WebDriver: having trouble selecting an element

Hi All,

I am having issues trying to select an element.

<tr data-code="ABCfix10.8">
<td>[UNRELATED CODE]</td>
<td>
  <a href="https://unusable-dynamic-link.com" class="unusable-generic-class">Apply</a>
</td>
</tr>

Basically I need to be able to find the tr that has a data-code attribute that starts with “ABC” and then click on the Apply link.

All other attributes (ID/class/link) are either dynamically generated or generic so cannot be used. The only static attribute is this data-code attribute.

Any help appreciated!

Thanks

sad_muso

Hi,

Have you tried xpath to find the element of interest? If you use Chrome, you can do inspect element and from the right-mouse-click context menu, you can copy the xpath value of the element. This is really quick and easy in comparison to trying to work it out yourself.

This link to SO might help.Example of using by xpath and attribute.

Find elements by attribute

So, maybe something like…
webDriver.findElements(By.xpath("//element[@data-code']"))

and then iterate over the elements until one has an id that starts with ‘ABC’.

1 Like

The way to create path for above element is as follows:

//tr[contains(@data-code,‘ABC’)]//a[text()=‘Apply’]

Automation testing using Selenium tool allows you to find a element using it attribute with value which is either equals or partially contains. So, as per your requirement, tag ‘tr’ which has ‘data-code’ attribute and its value always contains ‘ABC’.
Then assuming ‘td’ followed by ‘a’ are children of ‘tr’. Hence, directly navigate to ‘a’ by using double slashes. Here, you need to verify text in tag ‘a’ is equal to ‘Apply’.

The same is working for us. Please try this and let us know in case you need any other help.

3 Likes