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

Mohammed
  • 334
  • 1
  • 5
  • 22

1 Answers1

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']
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • I tried "driver.find_elements_by_xpath('//*[contains(text(), "Uthyres av")]/parent::*/text()')" it didn't work – Mohammed May 26 '17 at 21:27
  • 1
    It's possible that you are trying to find elements before it loads up. Try adding a wait before find_elements_..., see this [answer](https://stackoverflow.com/questions/7781792/selenium-waitforelement). – Psidom May 26 '17 at 21:32
  • I got this error The result of the xpath expression "//*[contains(text(), "Uthyres av")]/parent::*/text()" is: [object Text]. It should be an element. – Mohammed May 26 '17 at 22:08