Python Selenium: Finds h1 element but returns empty text string
As @ahmad-moussa mentioned, for me to the solution was:
import time
(...)
time.sleep(1)
# before
<webelement>.text
The problem is that there are two h1
elements with totally the same outer HTML
: the first is hidden, the second is not. You can check it with
print(len(driver.find_elements_by_xpath('//h1[@class="product-title "]')))
text
property allow you to get text from only visible elements while textContent
attribute also allow to get text of hidden one
Try to replace
new_name = driver.find_element_by_xpath(xp_name).text
with
new_name = driver.find_element_by_xpath(xp_name).get_attribute('textContent')
or simply handle the second (visible) header:
driver.find_elements_by_xpath('//h1[@class="product-title "]')[1].text