[英]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">€ 22.014,54</span>
But it prints : € —
但它打印:
€ —
while I want it to print what is actually there: € 22.014,54
虽然我希望它打印实际存在的内容:
€ 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.