Selenium无法按ID定位元素



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

然而,Selenium给了我一个错误:

"selenium.com.mon.exceptions.NoSuchElementException:消息:无法定位元素:[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

这是一个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'
)

您尝试过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'))

最新更新