简体   繁体   English

selenium find_elements_by_xpath .get_attribute 没有显示正确的值

[英]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;—但它打印: €&nbsp;—

while I want it to print what is actually there: €&nbsp;22.014,54虽然我希望它打印实际存在的内容: €&nbsp;22.014,54

So it turns the value "22.014,54" into "-".所以它将值“22.014,54”变成“-”。

How do I get the original value instead of "-"?如何获得原始值而不是“-”?

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:您可以使用time.sleep来做到这一点:

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.但是使用time.sleep并不是一个好的做法。 Instead I would advice you to use WebDriverWait .相反,我建议您使用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]'))) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python selenium中用get_attribute()找到xpath - Find the xpath with get_attribute() in python selenium 硒的问题-find_elements_by_xpath或find_elements_by_tag - issue with selenium - find_elements_by_xpath or find_elements_by_tag Python - Web 抓取 - Selenium - AttributeError: 'WebDriver' object 没有属性 'find_elements_by_xpath - Python - Web Scraping - Selenium - AttributeError: 'WebDriver' object has no attribute 'find_elements_by_xpath python/Selenium --&gt; find_elements_by_xpath 方法找不到所有元素 - python/Selenium --> find_elements_by_xpath method not finding all elements Python Selenium 网页抓取:find_elements_by_xpath 返回一个空列表 - Python Selenium Webscraping: find_elements_by_xpath returning an empty list find_elements_by_xpath 的第二次迭代在 selenium python 中给出错误 - Second iterative of find_elements_by_xpath gives error in selenium python 如何使用 Selenium 的“find_elements_by_xpath”打印标题 - How do I get the titles to print using Selenium's "find_elements_by_xpath" AttributeError: 'WebDriver' object 在机器人框架中没有属性 'find_elements_by_xpath' - AttributeError: 'WebDriver' object has no attribute 'find_elements_by_xpath' in robotframework Python:Selenium 驱动程序 find_elements_by_xpath:问题 - Python: Selenium Driver find_elements_by_xpath: Issue Python:硒:find_elements_by_xpath 最大 10 - Python: Selenium: find_elements_by_xpath max 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM