XPath to locate an element containing text nodes?

Viewed 37

I'm trying to locate the content from <div class="initiative-section__content">...</div> by using this XPath "//*[contains(text(),'Created')]/following-sibling::*", but it's returning two elements since there is also "Created Date". And I can't figure out how to add "By" to XPath. I tried something like this but it didn't work "//*[contains(text(),'Created') and contains(text(),'By')]/following-sibling::*"

Any ideas?

<div class="initiative-section__title">
   "Created"
   " By"
</div>
<div class="initiative-section__content">...</div>
1 Answers

Test the space-normalized string-value of the div element rather than its children text nodes:

//div[normalize-space() = "Created By"]/following-sibling::*

Note that you might want to narrow the following sibling test to specify the immediate sibling,

//div[normalize-space() = "Created By"]/following-sibling::*[1]

or even the first immediate sibling that must be a div element:

//div[normalize-space() = "Created By"]/following-sibling::*[1][self::div]
Related