python学习之Unable to locate element

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys  #需要引入keys包
import os,time

driver = webdriver.Chrome()
driver.get("http://****/ims_smart_web/index.html#metricQuery")

time.sleep(3)
driver.maximize_window() # 浏览器全屏显示

driver.find_element_by_id("j_username").clear()
driver.find_element_by_id("j_username").send_keys("用户名")

#tab的定位相相于清除了密码框的默认提示信息,等同上面的clear()
driver.find_element_by_id("j_username").send_keys(Keys.TAB)
time.sleep(3)
driver.find_element_by_id("j_password").send_keys("密码")

#通过定位密码框,enter(回车)来代替登陆按钮
driver.find_element_by_id("j_password").send_keys(Keys.ENTER)
'''
#也可定位登陆按钮,通过enter(回车)代替click()
driver.find_element_by_id("login").send_keys(Keys.ENTER)
'''
time.sleep(3)

time.sleep(3)
driver.quit()

  

Traceback (most recent call last):
File "D:\aasoftcfl\python\cflproject\chrome_12_1.py", line 21, in <module>
driver.find_element_by_id("password").send_keys(Keys.ENTER)
File "D:\aasoftcfl\python\lib\site-packages\selenium-3.11.0-py3.6.egg\selenium\webdriver\remote\webdriver.py", line 351, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "D:\aasoftcfl\python\lib\site-packages\selenium-3.11.0-py3.6.egg\selenium\webdriver\remote\webdriver.py", line 955, in find_element
'value': value})['value']
File "D:\aasoftcfl\python\lib\site-packages\selenium-3.11.0-py3.6.egg\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "D:\aasoftcfl\python\lib\site-packages\selenium-3.11.0-py3.6.egg\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"password"}
(Session info: chrome=66.0.3359.139)
(Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86_64)

问题出现原因:不存在id为password的元素,id应该为j_password,修改为j_password就可以跑通了

 

学习虫师的轻松自动化:

http://www.cnblogs.com/fnng/p/3258946.html

posted @ 2018-05-22 15:35  依羽杉  阅读(1483)  评论(0编辑  收藏  举报