zoukankan      html  css  js  c++  java
  • 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:aasoftcflpythoncflprojectchrome_12_1.py", line 21, in <module>
    driver.find_element_by_id("password").send_keys(Keys.ENTER)
    File "D:aasoftcflpythonlibsite-packagesselenium-3.11.0-py3.6.eggseleniumwebdriver emotewebdriver.py", line 351, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
    File "D:aasoftcflpythonlibsite-packagesselenium-3.11.0-py3.6.eggseleniumwebdriver emotewebdriver.py", line 955, in find_element
    'value': value})['value']
    File "D:aasoftcflpythonlibsite-packagesselenium-3.11.0-py3.6.eggseleniumwebdriver emotewebdriver.py", line 312, in execute
    self.error_handler.check_response(response)
    File "D:aasoftcflpythonlibsite-packagesselenium-3.11.0-py3.6.eggseleniumwebdriver emoteerrorhandler.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

  • 相关阅读:
    sql server 索引总结一
    公用表表达式(CTE)
    解决Setting property 'source' to 'org.eclipse.jst.jee.server的问题
    python测试开发django-2.templates模板与html页
    python测试开发django-1.开始hello world!
    pytest文档28-重复执行用例(pytest-repeat)
    pytest文档27-pytest分布式执行(pytest-xdist)
    pytest文档26-运行上次失败用例(--lf 和 --ff)
    pytest文档25-conftest.py作用范围
    python接口自动化11-post传data参数案例
  • 原文地址:https://www.cnblogs.com/shishibuwan/p/9072347.html
Copyright © 2011-2022 走看看