HTML XPath Searching by class and text
Presuming the conditions below:
- The class attribute contains only
myclass
- The innerText contains
qwerty
(without any leading and trailing spaces)
You can use the following solution:
//*[@class='myclass' and text()='qwerty']
Incase the innerText contains qwerty
along with some leading and trailing spaces, you can use either of the following solutions:
Using
normalize-space()
://*[@class='myclass' and normalize-space()='qwerty']
Using
contains()
://*[@class='myclass' and contains(., 'qwerty')]
//span[contains(@class, 'myclass') and text() = 'qwerty']
or
//span[contains(@class, 'myclass') and normalize-space(text()) = 'qwerty']
Generic solution for anyone looking for a XPath expression to search based on class and text:
Class is only "myclass":
//*[@class='myclass' and contains(text(),'qwerty')]
Class contains "myclass":
//*[contains(@class,'myclass') and contains(text(),'qwerty')]