Olivier
Olivier

Reputation: 95

selenium find_elements_by_xpath .get_attribute does not show proper value

I use the following code:

    driver = webdriver.Chrome(executable_path = r'G:\Program Files\chromedriver')
    
    driver.get("https://trader.degiro.nl/login/nl?_ga=2.48166567.1674906184.1604234065-1030449807.1604234065#/login")
    
    driver.implicitly_wait(2)
    
    username = driver.find_element_by_id("username")
    username.clear()
    username.send_keys("not actual info")
    
    password = driver.find_element_by_name("password")
    password.clear()
    password.send_keys("not actual info")
    
    
    driver.find_element_by_name("loginButtonUniversal").click()
    

    #top nav elements
    elems = driver.find_elements_by_xpath("/html/body/div[1]/div/div[2]/div[1]/div/div/button/span[1]")
    
    for e in elems:
        print(e.get_attribute('innerHTML'))

To get to this element:

<span data-id="totalPortfolio" data-field="total" 
class="uJDZaBS4 " data-positive="true" 
title="22.014,54">€&nbsp;22.014,54</span>

But it prints :€&nbsp;—

while I want it to print what is actually there: €&nbsp;22.014,54

So it turns the value "22.014,54" into "-".

How do I get the original value instead of "-"?

Upvotes: 1

Views: 294

Answers (1)

Sushil
Sushil

Reputation: 5531

You have to wait after clicking on the login button for the page to load. You can use time.sleep in order to do that:

driver.find_element_by_name("loginButtonUniversal").click()
time.sleep(3)
elems = driver.find_elements_by_xpath("/html/body/div[1]/div/div[2]/div[1]/div/div/button/span[1]")

But using time.sleep is not considered as a good practice. Instead I would advice you to use WebDriverWait. Here is how you use it:

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

driver.find_element_by_name("loginButtonUniversal").click()

elems = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '/html/body/div[1]/div/div[2]/div[1]/div/div/button/span[1]'))) 

Upvotes: 1

Related Questions