I am trying to get the text in the header on this page:
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')]]"
Retrieving via .text
in Selenium WebDriver for Python:
new_name = driver.find_element_by_xpath(xp_name).text
The driver finds the xpath, but when I print new_name
, macOS Terminal only prints a blank string: ""
What could be the reason for this?
Note: I also tried some other xpath alternatives, getting the same result, for example with:
xp_name = ".//*[@id='fundHeader']//h1"
How to get string text value from tags in selenium python with Examples? You can easily extract the text value of the h1 tag if you remember only two steps. get text of returned object of result of running locating method you can find all h1 tags by find_elements_by_xpath () method. If you don’t know xpath syntax, please read another example below.
Now that you know what inner text and hidden by CSS means, let us iterate our definition which says, getText () method in Selenium fetches the inner text of an element, which is not hidden by CSS and returns it as a String value. In simple words, whatever is displayed as text on the browser will be returned as-is by the getText () method.
Step 1: First, import the libraries, selenium, and time. Step 3: Next, establish a connection with the web driver through the executable path. Step 4: Now, obtain the website in which you want to find the element.
button = driver.find_element_by_xpath (“//button [contains ( text ( ), ‘Geeks for Geeks’)]”) Step 1: First, import the libraries, selenium, and time. Step 3: Next, establish a connection with the web driver through the executable path. Step 4: Now, obtain the website in which you want to find the element.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With