Selenium 无法通过 ID 定位元素

Selenium Unable to Locate Element by ID

我正在尝试在我的 CC 登录页面中输入用户名(和密码)。

但是,Selenium 给我错误:

"selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="username"]".

在尝试识别用户名 ID 之前,我让页面加载了 5 秒,但这没有帮助。我也曾尝试通过 xpathname 进行识别,但没有成功。有人知道可能出了什么问题吗?

这是我尝试登录的 CC 网页。

到目前为止,这是我的代码:

def login(url, usernameId, username, passwordId, password, submit_buttonId):
   driver.get(url)
   time.sleep(5)
   driver.find_element_by_id(usernameId).send_keys(username) #username input box not being identified
   driver.find_element_by_id(passwordId).send_keys(password)
   driver.find_element_by_xpath(submit_buttonId).click()

login(
'https://cards.barclaycardus.com/',
'username', myBarclaysUsername,
'password', myBarclaysPassword,
'loginButton'
)
<iframe id="login-iframe" title="Login" src="https://www.barclaycardus.com/servicing/authenticate/home?rnd=120231985&amp;xsessionid=FF4CC3BDD157751E5B9AF5E756D3E303" frameborder="0"></iframe>

您有一个 iframe 切换到它。

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "login-iframe")))

导入

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

你试过wait.until了吗?看看这样的东西是否有效:

from selenium import webdriver
from selenium.webdriver.support import ui

driver = webdriver.Chrome()
driver.get(url)
wait.until(lambda driver: driver.find_element_by_id('username'))

有一个 iframe 所以你必须切换到它

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

def login(url, usernameId, username, passwordId, password, submit_buttonId):
   driver.get(url)
   WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "login-iframe")))

   time.sleep(5)

   driver.find_element_by_id(usernameId).send_keys(username) #username input box not being identified
   driver.find_element_by_id(passwordId).send_keys(password)
   driver.find_element_by_xpath(submit_buttonId).click()

login(
'https://cards.barclaycardus.com/',
'username', myBarclaysUsername,
'password', myBarclaysPassword,
'loginButton'
)