Writing an XPath element that contains a textual substring
By adding contains()
to the selector, the object can be found with just a small part of the text:
//a[contains(text(),'Next']
This works for many elements, but checkboxes and radio buttons are tricky.
Finding an element relative to another element
In this next example, we want to click the checkbox next to the name John Smith. We have several checkboxes but none has a unique identifier by name:
Figure 10.4 – The name John Smith and its associated checkbox are two separate elements
This selector will identify all the checkboxes:
//input[contains(@name,'chkSelect')]
To locate this checkbox element, we need to use the link and find the input checkbox preceding it. Here’s how we can do that:
//a[normalize-space()='John.Smith']//preceding::input[@id='ohrmList_chkSelectRecord_2']
Case-insensitive partial matches
What if we...