Brandon Jacobson
Brandon Jacobson

Reputation: 159

How to extract text from Selenium Object?

My code logs into my Fidelity account and then finds an element. I'm able to find the Selenium object, but I want to extract the text in it which contains my portfolio balance. When I print the object, the code works fine, but I'm unable to get the text or the balance from it.

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


def fidelity_check():
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("detach", True)
    # options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Users\Notebook\Documents\chromedriver.exe')
    driver.get("https://www.fidelity.com/")
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#userId-input"))).send_keys(
        "MYUSERID")
    driver.find_element_by_css_selector("input#password").send_keys("MYPASSWORD")
    driver.find_element_by_css_selector("button#fs-login-button").click()

    try:
        element_present = EC.presence_of_element_located((By.XPATH, '// *[ @ id = "tabContentSummary"]'))
        WebDriverWait(driver, 10).until(element_present)
        print(element_present)
    except TimeoutError:
        print("Page Not Loaded.")

This is the result: selenium.webdriver.support.expected_conditions.presence_of_element_located object at 0x035EFE10

Here is the inspect element results:

<span class="ledger--section-total js-total-balance-value">$X.XX</span>

I tried the following and it doesn't work.

element_present.text()
# And I tried:
element_present.text

Neither work.

Upvotes: 0

Views: 636

Answers (2)

KunduK
KunduK

Reputation: 33384

You have to assign the value in a variable from WebDriverWait and then get the text.What you have done you have printed selenium object.

element_present = EC.presence_of_element_located((By.XPATH, '// *[ @ id = "tabContentSummary"]'))
element=WebDriverWait(driver, 10).until(element_present)
print(element.text)

EDIT:

element_present = EC.visibility_of_element_located((By.XPATH, '// *[ @ id = "tabContentSummary"]'))
element=WebDriverWait(driver, 20).until(element_present)
print(element.text)

OR

element_present = EC.element_to_be_clickable((By.XPATH, '// *[ @ id = "tabContentSummary"]'))
element=WebDriverWait(driver, 20).until(element_present)
print(element.text)

Upvotes: 1

SeleniumUser
SeleniumUser

Reputation: 4177

please try below solution

amount = wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id = "tabContentSummary"]//span')))
print(amount.text)

Upvotes: 1

Related Questions