Python Selenium: finds h1 element but returns an empty text string

I am trying to get the text in the title on this page :

enter image description here

iShares FTSE MIB UCITS ETF EUR (Dist.)

The tag looks like this:

<h1 class="product-title" title="iShares FTSE MIB UCITS ETF EUR (Dist)"> iShares FTSE MIB UCITS ETF EUR (Dist) </h1>

I am using this xPath:

xp_name = ".//*[@class[contains(normalize-space(.), 'product-title')]]"

Getting through .textin Selenium WebDriver for Python:

new_name = driver.find_element_by_xpath(xp_name).text

The driver finds xpath, but when I type new_name, the MacOS terminal only prints an empty line:""

What could be the reason for this?

enter image description here


Note: I also tried some other xpath alternatives, getting the same result, for example with:

xp_name = ".//*[@id='fundHeader']//h1"
+6
source share
2 answers

, h1 HTML: , - .

print(len(driver.find_elements_by_xpath('//h1[@class="product-title "]')))

text , textContent

new_name = driver.find_element_by_xpath(xp_name).text

new_name = driver.find_element_by_xpath(xp_name).get_attribute('textContent')

() :

driver.find_elements_by_xpath('//h1[@class="product-title "]')[1].text
+10

.

<p class="errorMessholder ng-binding" ng-bind="errLogin.Server" ng-show="errLogin.ServerFlag">Authentication failed.</p>

XPath CSS Selector:

  1. auth_error = driver.find_element_by_xpath("//p[@ng-bind='errLogin.Server']") #XPATH
  2. auth_error= driver.find_element_by_css_selector("[ng-bind='errLogin\.Server']") #CSS Selector

.text * .get_attribute Selenium WebDriver Python:

  1. x = auth_error.get_attribute('textContent')

    print(x)

  2. x = auth_error.text

    print(x)

XPath, 'x', Eclipse Terminal : ""

print(len(auth_error)) 0.

, .

, .

0

Source: https://habr.com/ru/post/1674890/


All Articles