Akira
Akira

Reputation: 2870

Element not found by name/id on selenium

I am using Selenium to login in Mathwork account but have this error message "AttributeError: 'NoneType' object has no attribute 'send_keys'"

Here is the source of login page of Mathwork:

name

and

enter image description here

I have tried to different lines of code below, but to no avail:

username = driver.find_element_by_xpath(".//*[@id='userId']")
username = driver.find_element_by_name('userId')
username = driver.find_element_by_id('userId')

Here is my full code:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('C:\\Users\Dung Le\\Downloads\\Compressed\\chromedriver.exe')

driver.get('https://www.mathworks.com/login?uri=https%3A%2F%2Fwww.mathworks.com%2Fhelp%2Findex.html%3Fs_tid%3DCRUX_lftnav')
driver.implicitly_wait(60)
username = driver.find_element_by_name('userId')
username.send_keys('my_email')

password = driver.find_element_by_name('password')
time.sleep(2)
password.send_keys('my_password')

I received this error:

"C:\Program Files\Python37\python.exe" "C:/Users/Dung Le/PycharmProjects/untitled7/dsd.py" Traceback (most recent call last): File "C:/Users/Dung Le/PycharmProjects/untitled7/dsd.py", line 11, in username.send_keys('leanh***@gmail.com') AttributeError: 'NoneType' object has no attribute 'send_keys'

Process finished with exit code 1

and this output:

enter image description here

I expect to resolve this error and has my login information in the input space of the login page.

Thank you for your help!

Upvotes: 0

Views: 690

Answers (1)

SEDaradji
SEDaradji

Reputation: 1441

You should always check if the element is in the main content or in a frame, if the element is in a frame, you have to switch to that frame first:

frame = driver.find_element_by_id('me')
driver.switch_to.frame(frame)
driver.find_element_by_id('userId').send_keys('blablabla')

after you are done with the frame, switch back to the default content

driver.switch_to.default_content()

Upvotes: 2

Related Questions