0

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.

4
  • Have you tried element_present.innerHTML? Commented Nov 16, 2019 at 14:39
  • Can you please provide complete DOM ? Commented Nov 16, 2019 at 14:46
  • @Arn, I just tried it and it didn't work. I just edited it for the whole code. Maybe calling it my XPATH isn't the right way since I can't use the .text or .innerHTML methods. Commented Nov 16, 2019 at 14:49
  • @Rock, I just posted the full code. Commented Nov 16, 2019 at 14:50

2 Answers 2

1

please try below solution

amount = wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id = "tabContentSummary"]//span')))
print(amount.text)
Sign up to request clarification or add additional context in comments.

1 Comment

another person posted before with a similar variation. It works perfectly. The program calls it too quickly, so it says Loading...instead of the balance. I need to delay a little. Thank you so much for your help!
1

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)

2 Comments

this worked!! It returned Loading... because my computer is quick, so I just need to delay it a little until the balance is shown. Thanks so much for the reply.
@BrandonJacobson : Can you check edited option.I believe you dont need to add delay using my Edited option.Give it a try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.