Failed to interact with keybord and .getAttribute returning null for data-* attribute when I tried to login gmail automatically using python and selenium
I was trying to login gmail automatically using python and selenium. The first step which is sending keys to username field was successful but there is a error raised when I tried to send keys to password field: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementNotInteractableException: Message: Element is not reachable by keyboard
This is for a latest selenium and python 3
here is my python code:
def fill_form():
driver = webdriver.Firefox()
#browser initiallized
driver.maximize_window()
driver.get("https://gmail.google.com")
driver.implicitly_wait(20) #gives an implicit wait for 20 seconds
# find username and password element in browser
print ("-----------------login------------------")
EMAIL = driver.find_element_by_xpath('//*[@id="identifierId"]')
EMAIL.send_keys("#my mail")
next_stepBTN = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/span/span')
next_stepBTN.click()
driver.implicitly_wait(20) #gives an implicit wait for 20 seconds
PASSWORD = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/span/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input')
print("found")
driver.implicitly_wait(20) #gives an implicit wait for 20 seconds
PASSWORD.send_keys("#my password") #failed here
print("send_keys")
I was looking for some other problems which is similar to mine and some people suggest that using JS console can locate element more precisely. Then I tried to locate password input and change it value in console. It went well at the beginning, which I can find the element; but it returns null when I getAttribute
var element = document.getElementsByClassName("whsOnd zHQkBf")[0]
element
# console result is below
<input class="whsOnd zHQkBf" type="text" jsname="YPqjbf" autocomplete="off" spellcheck="false" tabindex="0" aria-label="Please enter your password" name="password" autocapitalize="off" dir="ltr" data-initial-dir="ltr" data-initial-value="" badinput="false">
element.getAttribute("data-initial-value")
# And this is reurning null
I can see "data-initial-value" is changable when I enter something in the password field but element.getAttribute("data-initial-value") always return null.
I expect the password can be entered by the keyboard automatically using selenium or JS. I tried many ways and I can't see what the logic of login is. Anyone???
Well it seems to be you are trying to enter password before the next page loaded. That's the reason why you are getting the error message when script try to enter value in the hidden password field.
Try using the below code with imports.
# add the below imports
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# now write your logic to enter user name
# click on next button
# wait for the password field to display
password = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.CSS_SELECTOR,"[name='password'']")))
password.send_keys("your password")
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments