1
<div class="info">
    <span class="label">Establishment year</span>
    "2008"
</div>

I want to extract 2008 by using xpath but the expression just selects the establishment text.

driver.find_element_by_xpath("//*[text()='Establishment year']")
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Prakhar T
  • 47
  • 3

2 Answers2

1

As the text 2008 is within a text node to extract the text 2008 you can use the following solution:

print(driver.execute_script('return arguments[0].lastChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='info']/span[@class='label' and text()='Establishment year']/..")))).strip())
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • I only want to extract 2008 and not the text 'Establishment year' – Prakhar T Jul 17 '19 at 10:15
  • @PrakharT What is the output you are getting once you incorporate this line of code? – DebanjanB Jul 17 '19 at 10:19
  • can you please share some tutorial for driver.execute_script function? – Prakhar T Jul 17 '19 at 10:41
  • @PrakharT Here a few good discussions to start with to understand `execute_script ()` function: [this](https://stackoverflow.com/questions/52689880/what-is-javascriptexecutor-in-selenium/52698089#52698089) and [this](https://stackoverflow.com/questions/49871432/what-does-arguments0-and-arguments1-mean-when-using-executescript-method-fro/49873090#49873090) – DebanjanB Jul 18 '19 at 06:39
1

Unfortunately WebDriver does not allow find_element function result to be a Text Node so you will have to go for execute_script function like:

driver.execute_script(
"return  document.evaluate(\"//div[@class='info']/node()[3]\", document, null, XPathResult.STRING_TYPE, null).stringValue;")

Demo:

enter image description here

More information:

Dmitri T
  • 119,313
  • 3
  • 56
  • 104