Selenium WebDriver: having trouble selecting an element

(James) #1

Hi All,

I am having issues trying to select an element.

<tr data-code="ABCfix10.8">
  <a href="" class="unusable-generic-class">Apply</a>

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!



(Chris) #2


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…

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

(Vishal Dutt) #3

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


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.