I am trying to get the text contents of <p>
containing a particular text, using Selenium for Python.
My code works for most pages where I deploy it, but not for this particular page and some others I have encountered.
Where the code otherwise returns the text contents of the found <p>
, here it does find the element but returns what seems to be an empty string.
What could be causing this?
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://advisors.vanguard.com/VGApp/iip/site/advisor/investments/productoverview?fundId=4415")
match_string = "seeks to track the"
elmnt = driver.find_element_by_xpath((
"//*[self::p or self::span or self::div]" +
"[text()[contains(., '%s')]]" % match_string
)).text
print "Result:" + elmnt
Part of page HTML, where I want to get the text in <p>
:
<div style="margin:0px;">
<h2 style="margin-bottom:8px" class="option1"><!--PPE:Content-188-->Summary of this fund<!--End PPE--></h2>
<p>Vanguard International Dividend Appreciation ETF seeks to track the performance of a benchmark index that measures the investment return of non-U.S. companies that have a history of increasing dividends.</p>
</div>