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()
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')))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With