Failed to interact with keybord and .getAttribute returning null for data-* attribute using python and selenium

summerslumber

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???

supputuri

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.

enter image description here

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.

edited at
0

Comments

0 comments
Login to comment

Related

Why is .getAttribute returning null for data-* attribute

Vuetify - getAttribute of data attribute in a component returns null

Javascript getAttribute("innerHTML") returning null?

this.getAttribute('attribute') returning null even though attributes showing in this.attributes

How to extrac data from tag attribute using Selenium and Python?

Python selenium - set value using data-qa attribute

How to interact with button based on text using Selenium and Python

How to interact with default slider on a webpage using javascript or selenium- Python

Getting 'null' by using the getAttribute() Method

How does __setattr__ and __getattribute__ interact in Python?

Interact with Chart JS with Python Selenium

Interact with React chart with Python Selenium

JavaScript returning NULL/Undefined when trying to get a data attribute

How to get attribute value using Selenium - Python

Return UID attribute value using Selenium Python

Data attribute is returning undefined

Returning data attribute values

Failed to open a website by using python Selenium

Find an element where data-tb-test-id attribute is present instead of id using Selenium and Python

How to get values of HTML tags (getAttribute is returning NULL)

getAttribute isPermaLink is returning null for element in rss feed with Protractor

why is jquery "this" attribute returning null?

interact elements with dynamic label in python selenium

Python Selenium - Interact with the next WebElement object in a list

Can't interact with elements in Selenium on Python

Selenium + Python - Not able to interact/click with JavaScript

How to find and interact with multiple elements within multiple frames on reCAPTCHA demo page using Selenium and Python

Selenium script is returning null to the element

Interact with web pages using python