python selenium驱动程序中的“消息:未知错误:无法聚焦元素”

时间:2016-06-19 13:59:33

标签: python selenium selenium-webdriver

为什么这段代码会抛出异常selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element

据我所知,我选择了正确的元素。谷歌搜索建议在发送密钥之前使用.click()元素,但这也无济于事。

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://netbanking.hdfcbank.com/netbanking/")
login_wait = WebDriverWait(driver, 10)

assert "Welcome to HDFC Bank" in driver.title

frame = login_wait.until(EC.presence_of_element_located((By.NAME, 'login_page')))
driver.switch_to.frame(frame)

try:
    elem = login_wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'pwd_field')))
    print("Page is ready!")
    elem.send_keys("123456")
    elem.send_keys(Keys.RETURN)
except TimeoutException:
    print("Loading took too much time!")

driver.close()

1 个答案:

答案 0 :(得分:8)

这是因为pwd_field类名称所定位的元素 - 您实际上有一个匹配定位符的span元素。相反,您打算使用密码input元素:

elem = login_wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'input_password')))