I was looking for an answer to how to find an element that has a class and contains text.
I've got two answers.
//div[@class='credit_summary_item' and contains(text(),'Professor']
:
as in HTML XPath Searching by class and text//div[contains(@class, 'credit_summary_item') and contains(., 'Professor')]
:
as in XPath to match @class value and element value?For me, only 2nd answer worked. Can anyone pls explain the difference for 'contains text' part.? As both answers don't mention it.
For a demonstration consider the following HTML:
<div class="credit_summary_item">Professor</div>
There is:
credit_summary_item
.So, to locate this element you can use either of the following solutions:
Using text()
:
//div[@class='credit_summary_item' and text()='Professor']
Using contains()
:
//div[@class='credit_summary_item' and contains(., 'Professor')]
But in your usecase it seems contains(@class, 'credit_summary_item')
worked which implies the element have multiple classes. So apart from credit_summary_item
there are some other values present as class attributes.