P A N
P A N

Reputation: 5922

Python/Selenium: Whitespace issue when retrieving text content from XPath (normalize-space)

I am having some difficulties with a relative XPath web scraper implementation with Selenium for Python.

From this Börse Frankfurt web page, I want to get the text in the cell adjacent to <td> UCITS IV-Konform </td>, namely the text in the cell that says <td class="text-right"> Ja </td>.

I have tested the XPath I'm using with Freeformatter which states that my XPath is correct.

Navigation to the page works fine. However, when I try to retrieve the text content, I get None. Apparently, it's not finding the XPath.

Post-answer edit: The issue is due to whitespace leading/trailing the text content.


from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Firefox()
driver.get("http://www.boerse-frankfurt.de/etp/db-x-trackers-STOXX-GLOBAL-SELECT-DIVIDEND-100-UCITS-ETF-1D-LU0292096186")

try:
    find_value = driver.find_element_by_xpath("//td[text()=' UCITS IV-Konform ']/following-sibling::td").text
except NoSuchElementException:
    find_value = None

print find_value

Upvotes: 1

Views: 1759

Answers (2)

Graham
Graham

Reputation: 135

Try using the contains function in your xpath:

"//td[contains(text(), 'UCITS IV-Konform')]/following-sibling::td"

There is a good explanation of this here.

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167641

Try the XPath "//td[normalize-space(.) = 'UCITS IV-Konform']/following-sibling::td" as I think there is a lot of leading and trailing white space in that cell.

Upvotes: 1

Related Questions