如何在Python中使用Selenium Webdriver填写https://www.discover.com/信用卡帐户中的用户名和密码字段?

问题描述 投票:0回答:1

我正在尝试创建一个脚本,该脚本会自动抓取我的信用卡交易数据并将其保存在excel中。提交凭据后,发现错误,提示我当前无法访问我的帐户。我怀疑Discover阻止了我的自动登录?是否有解决方法?我的代码在下面,并且错误消息作为图像附加。

import pandas as pd
import numpy as np
import selenium
import time
from selenium import webdriver

#log into Discover
driver = webdriver.Firefox()
driver.get('https://www.discover.com/')
time.sleep(2)

# Select the User ID text box
id_box = driver.find_element_by_id("userid-content")

# Send id information
id_box.send_keys('my_username')

# Select the password text box
password_box = driver.find_element_by_id('password-content')

# Send password information
password_box.send_keys('my_password')
password_box.submit()

发现登录错误消息

enter image description here

python selenium xpath css-selectors webdriverwait
1个回答
0
投票

要将字符序列发送到用户ID密码字段,您需要为visibility_of_all_elements_located()引入WebDriverWait,并且可以使用以下Locator Strategies中的任何一个:

  • 使用CSS_SELECTOR

    driver.get('https://www.discover.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#userid-content"))).send_keys('zh123')
    driver.find_element_by_css_selector("input#password-content").send_keys('zh123')
    
  • 使用XPATH

    driver.get('https://www.discover.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='userid-content']"))).send_keys('zh123')
    driver.find_element_by_xpath("//input[@id='password-content']").send_keys('zh123')
    
  • Note:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:

discover


其他注意事项

确保:

© www.soinside.com 2019 - 2024. All rights reserved.