我正在尝试在输入框中输入用户名,但它无效。该网站打开,我可以点击该框,但不会出现单词。将显示chrome developer扩展警告弹出窗口。我仍然是编程新手。有任何想法吗?感谢
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome()
browser.get('https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=13&ct=1478123673&rver=6.7.6631.0&wp=MBI&wreply=https%3a%2f%2fwww.bing.com%2fsecure%2fPassport.aspx%3frequrl%3dhttp%253a%252f%252fwww.bing.com%252f%253fwlexpsignin%253d1&lc=1033&id=264960')
time.sleep(5)
usernameElem = browser.find_element_by_class_name('phholder').click()
usernameElem.send_keys('username')
错误
Traceback (most recent call last):
File "C:/Users/Matt/PycharmProjects/untitled/Bing.py", line 10, in <module>
usernameElem.send_keys(Keys.ENTER)
AttributeError: 'NoneType' object has no attribute 'send_keys'
答案 0 :(得分:1)
您的选择器错误尝试使用xpath或id:请参阅下面的代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=13&ct=1478123673&rver=6.7.6631.0&wp=MBI&wreply=https%3a%2f%2fwww.bing.com%2fsecure%2fPassport.aspx%3frequrl%3dhttp%253a%252f%252fwww.bing.com%252f%253fwlexpsignin%253d1&lc=1033&id=264960')
time.sleep(5)
usernameElem = browser.find_element_by_xpath(".//*[@id='i0116']")
#Either use above xpath or use id
#usernameElem = browser.find_element_by_id("i0116")
usernameElem.send_keys('username')
答案 1 :(得分:0)
您应该对元素执行 send_keys 功能 因此,首先将元素分配给变量 usernameElem ,然后将其单击为函数,然后就可以将kes发送给 usernameElem 元素
usernameElem = browser.find_element_by_class_name('phholder')
usernameElem.click()
usernameElem.send_keys('username')