driver.find_element_by_id('username').send_keys('945412')
print 'username entered'
driver.find_element_by_name('password').send_keys('mns347')
print 'password entered'
driver.find_element_by_name("submit").click()
print 'submit'
但是它抛出错误
Traceback (most recent call last):
File "C:\Users\SS\Desktop\python-mp\sele_sample.py", line 17, in <module>
driver.find_element_by_id('username').send_keys('945412')
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"username"}
(Session info: chrome=70.0.3538.102)
(Driver info: chromedriver=2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90),platform=Windows NT 6.3.9600 x86_64)
答案 0 :(得分:1)
您可以添加显式等待来等待元素被加载。
username= WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.ID, "username"))
)
username.send_keys('945412')
答案 1 :(得分:1)
Link
答案 2 :(得分:1)
您需要安装chromedriver和chrome。您可以根据需要为其他浏览器更改这些设置。这对我来说很好。
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
<style>
.child1{
background-image:url(https://picsum.photos/200/300/?random);
width:300px;
height:300px;
}
.child2{
background-color: rgba(138, 43, 226, 0.6);
width:300px;
height:300px;
position: absolute;
top: 0px;
}
.parent {
position: relative;
height: 2000px;
}
</style>
答案 3 :(得分:0)
我看不到您的代码有任何错误。但是根据您的错误消息,没有ID用户名的字段。
请确保您的网站上确实存在一个带有ID用户名的字段。
例如,githubs用户名字段定义如下
<input name="login" id="login_field" class="form-control input-block" tabindex="1" autocapitalize="off" autocorrect="off" autofocus="autofocus" type="text">
在这种情况下,您将不得不对此进行调整。看起来像这样
driver.find_element_by_id('login_field').send_keys('945412')
因为该字段包含id =“ login_field”