硒无法按ID定位元素

时间:2020-11-09 01:03:09

标签: python selenium mozilla

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

但是,硒给了我错误:

“ selenium.common.exceptions.NoSuchElementException:消息:无法找到元素:[id =” username“]”。

在让我尝试识别用户名ID之前,我让页面加载了5秒钟,但这无济于事。我也尝试过用xpathname来识别,但是没有运气。任何人都有什么想法可能出问题了吗?

这是我要登录的抄送网页。

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

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'
)

3 个答案:

答案 0 :(得分:2)

<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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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