【问题标题】:Python selenium - Unable to capture and send_keys to the input boxPython selenium - 无法捕获并将_keys发送到输入框
【发布时间】:2022-01-14 14:39:53
【问题描述】:

我正在尝试自动登录网站https://plus.credit-suisse.com。下面的代码将我们带到应该输入密码的最后一步。

import time
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
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver = webdriver.Chrome()
driver.maximize_window()

driver.get("https://plus.credit-suisse.com")

wait = WebDriverWait(driver,10)
wait.until(EC.element_to_be_clickable((By.XPATH, '//button[text()="Sign-in"]'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@placeholder="Enter Credit Suisse ID (usually your registered email address)"]'))).send_keys("username")
wait.until(EC.element_to_be_clickable((By.XPATH, '//a[@class="mod_cookie_policy_btn_close"]'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//button[text()="Enter Password to sign-in"]'))).click()

time.sleep(5)

element = wait.until(EC.element_to_be_clickable((By.ID, 'password')))
element.send_keys('password')

wait.until(EC.element_to_be_clickable((By.XPATH, '//button[text()="Login"]'))).click()

现在行

element = wait.until(EC.element_to_be_clickable((By.ID, 'password')))

抛出 TimeoutException。这是最后一步。

【问题讨论】:

  • 您是否在没有"--headless" 的情况下对其进行了测试以查看实际显示的内容?
  • 我找到了 3 个 ID 为 password 的元素 - 这可能会造成问题

标签: python selenium web-scraping xpath


【解决方案1】:

我发现此页面有 3 个 ID 为 password 的元素。

最后一个元素是正确的,但element_to_be_clickable 可能会给出第一个。

您可能必须使用 XPath 来获得正确的路径

element = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="csplusSecondView"]//input[@id="password"]')))

【讨论】:

  • 经过测试,应该可以工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 2020-11-30
  • 2020-08-14
  • 2015-07-09
  • 2021-07-03
  • 2020-06-14
相关资源
最近更新 更多