0

I am having issues returning the text found through an xpath. First I needed to find the element through css selector:

ticker1 = browser.find_element_by_css_selector('[data-id="1"]')
print(ticker1.get_attribute('innerHTML'))

<div class="header_card"><div class="header_card_title">Day's Title<i class="fa fa-info-circle 
toolTrigger" data-title="Day's Title" data- 
id="DaysTitle-tooltip" data-model="what-is-this"></i></div><div 
class="header_card_trend_symbol headerTrend" data-title="" data-id="1" data-process="" 
data-categories="" data-datapoints=""><i class="fa fa-arrow-circle-o-down ph_trend_red"></i></div> 
<div class="header_card_metric_value" data-id="1"><span>$0</span></div></div><div 
class="header_bars_container"><a href="#" class="dropdown-toggle" data-toggle="dropdown" 
data-boundary="viewport" role="button" aria-haspopup="true" aria-expanded="false"><i class="fa fa- 
bars optionsIconDropDown"></i></a><ul class="dropdown-menu drop-sm" style="position: relative; right: 
10px; top: -16px !important; "><li></li><li><a href="#" class="headerTrend" data-title="Prior Day's 
Production" data-id="1" data-open="" data-process="" data-categories="" data-datapoints="">Trend</a> 
</li></ul></div>

I then access the sub-element through xpath

t1l = ticker1.find_element_by_xpath(".//div[@class='header_card_title']")
print(t1l.get_attribute('innerHTML'))

Day's Title<i class="fa fa-info-circle toolTrigger" data-title="Day's Title" 
data-id="PriorDaysProduction-tooltip" data-model="what-is-this"></i>

I have tried .text, get_attribute('text') , get_attribute('values') all return ''

DebanjanB
  • 118,661
  • 30
  • 168
  • 217

2 Answers2

0

figured it out,

it is get_attribute('innerText') although not sure why.

0

You were so close. Instead of get_attribute('innerHTML') you need to extract the text attribute as follows:

t1l = ticker1.find_element_by_xpath(".//div[@class='header_card_title']")
print(t1l.text)

Ideally, to print the text Day's Title you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

print(WebDriverWait(ticker1, 20).until(EC.visibility_of_element_located((By.XPATH, ".//div[@class='header_card_title']"))).text)

You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


Outro

Link to useful documentation:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217