在selenium中找不到元素ByName/ID

2024-03-28 16:24:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Selenium登录Mathwork帐户,但出现以下错误消息“AttributeError:'NoneType'对象没有属性'send\u keys'”

以下是Mathwork登录页面的来源:

name

以及

enter image description here

我尝试了以下不同的代码行,但没有效果:

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

这是我的完整代码:

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')

我收到这个错误:

"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

这个输出:

enter image description here

我希望解决这个错误,并在登录页面的输入空间中有我的登录信息。你知道吗

谢谢你的帮助!你知道吗


Tags: namefromimportsendbydriverselenium错误
1条回答
网友
1楼 · 发布于 2024-03-28 16:24:38

您应该始终检查元素是在主内容中还是在框架中,如果元素在框架中,则必须首先切换到该框架:

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

处理完框架后,切换回默认内容

driver.switch_to.default_content()

相关问题 更多 >