zoukankan      html  css  js  c++  java
  • Message:Unable to locate element 问题解决方法

    Python断断续续学了有一段时间了,总感觉不找个小项目练练手心里没底,哪成想出门就遇到“拦路虎”,一个脚本刚写完就运行报错,还好做足了心里准备,尝试自行解决。

    或许网上有相关解决方案,不过毕竟自己亲自上手实操了,有的问题还是有必要记录下来加深印象,于是就有了这篇短文。

    背景:访问禅道首页,通过第三方QQ账号与密码手动输入登陆。

    代码:

     1 # coding:utf-8
     2 from selenium import webdriver
     3 import time
     4 
     5 driver = webdriver.Firefox()
     6 driver.get('http://www.zentao.net/ ')
     7 
     8 driver.find_element_by_link_text('登录').click()
     9 driver.find_element_by_class_name('qq').click()
    10 driver.find_element_by_css_selector('#switcher_plogin').click()
    11 driver.find_element_by_id('u').send_keys('*******')
    12 driver.find_element_by_id('p').send_keys('*******')
    13 driver.find_element_by_id('login_button').click()
    14 time.sleep(10)
    15 
    16 driver.quit()

    学习了元素定位几种方式,所以利用link_text 、  class_name 、  css_selector 、  id四种方式,不过执行时却提示selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":"#switcher_plogin"}

    也就是说第10行代码有问题,不能通过css_selector方式定位到"#switcher_plogin"这个元素。

    虽然检查了元素的唯一性,也尝试通过其他如xpath 、 name等方式进行定位,但是使用运行到这块报错。仔细回想了之前看的示例登陆126邮箱的过程,有个iframe的方式进行定位,于是参考进行了尝试。

    不过由于对示例的没有深入了解,照猫画虎也出了糗。原例中查看是,相应代码是

    driver.switch_to.frame('x-URS-iframe')

    于是我在开头代码的第9至10行插入了相同代码,结果可想而知,依然报错,只是提示信息变成了Unable to locate element:x-URS-iframe。

    这时候再来查看禅道第三方qq登陆的地方,原来还是有区别的,于是修改相应的代码为

    driver.switch_to.frame('ptlogin_iframe')

    此时再运行才正常通过。

    虽然没什么技术难度,但还得心细,了解代码的真正含义,再去尝试写,不能照搬照抄。

    最后附上完整代码,为接下来的尝试做准备~

     1 # coding:utf-8
     2 from selenium import webdriver
     3 import time
     4  
     5 driver = webdriver.Firefox()
     6 driver.get('http://www.zentao.net/ ')
     7   
     8 driver.find_element_by_link_text('登录').click()
     9 driver.find_element_by_class_name('qq').click()
    10 driver.switch_to.frame('ptlogin_iframe')                             # 通过frame方式定位
    11 driver.find_element_by_css_selector('#switcher_plogin').click()
    12 driver.find_element_by_id('u').send_keys('*******')
    13 driver.find_element_by_id('p').send_keys('*******')
    14 driver.find_element_by_id('login_button').click()
    15 time.sleep(10)
    16 
    17 driver.quit()
  • 相关阅读:
    LSMW TIPS
    Schedule agreement and Delfor
    Running VL10 in the background 13 Oct
    analyse idoc by creation date
    New Journey Prepare
    EDI error
    CBSN NEWS
    Listen and Write 18th Feb 2019
    Microsoft iSCSI Software Target 快照管理
    通过 Microsoft iSCSI Software Target 提供存储服务
  • 原文地址:https://www.cnblogs.com/mrgavin/p/7610126.html
Copyright © 2011-2022 走看看