Selenium无法按ID定位元素

2024-10-04 11:23:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在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'
)

Tags: idurlbydriverusernameloginpassword页面
3条回答
<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'
)

相关问题 更多 >