0

I have the following HTML page and I am using Selenium under python to extract some data from the page HTML

<div class="secondary-content-col col-xs-12">
<div class="row">
<div class="col-xs-12">
<h2 class="h4"><span>Uthyres av:</span> Test</h2>
</div>
</div>
</div>

I want to get the Test text from tag, I have tried

driver.find_elements_by_xpath("//*[contains(., 'Uthyres')]")

but it says element no found! any idea how can I solve it

1 Answer 1

1

You could try this xpath:

//*[contains(text(), 'Uthyres')]/parent::*/text()

instead of contains(., ...) use contains(text(), ...) and then go to the parent node and extract the text. Note Test here is the text node of tag h2 instead of span.


Demonstration using lxml:

from lxml import etree

e = etree.fromstring("""<div class="secondary-content-col col-xs-12">
<div class="row">
<div class="col-xs-12">
<h2 class="h4"><span>Uthyres av:</span> Test</h2>
</div>
</div>
</div>""")

e.xpath('//*[contains(text(), "Uthyres")]/parent::*/text()')
# [' Test']
Sign up to request clarification or add additional context in comments.

3 Comments

I tried "driver.find_elements_by_xpath('//*[contains(text(), "Uthyres av")]/parent::*/text()')" it didn't work
It's possible that you are trying to find elements before it loads up. Try adding a wait before find_elements_..., see this answer.
I got this error The result of the xpath expression "//*[contains(text(), "Uthyres av")]/parent::*/text()" is: [object Text]. It should be an element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.