Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Message: unknown error: cannot focus element" in python selenium driver

Why does this piece of code throw an exception selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element?

As far as I can tell, I'm choosing the right element. Googling suggested to have a .click() on element before sending keys but that didn't help either.

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()
like image 221
Abhijeet Rastogi Avatar asked Jun 19 '16 13:06

Abhijeet Rastogi


1 Answers

This is because what element you've located by the pwd_field class name - you've actually got a span element matching the locator. Instead, you meant to get to the password input element:

elem = login_wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'input_password'))) 
like image 99
alecxe Avatar answered Nov 15 '22 08:11

alecxe