zoukankan      html  css  js  c++  java
  • selenium定位元素后,click不生效的问题。

    实现自动化登录微云账号。

     问题分析

     

    firefox_driver = webdriver.Firefox()
        firefox_driver.get("https://www.weiyun.com/")
        firefox_driver.implicitly_wait(10)
     # 登录账号
        firefox_driver.switch_to.frame("qq_login_iframe")
        firefox_driver.find_element_by_css_selector("#switcher_plogin").click()
        # print(s.text)
        input_name = firefox_driver.find_element_by_css_selector("#u")
        input_name.clear()
        input_name.send_keys("***")
        input_password = firefox_driver.find_element_by_css_selector("#p")
        input_password.clear()
        input_password.send_keys("****")
        firefox_driver.find_element_by_css_selector("#login_button").click()
        time.sleep(3)
        firefox_driver.quit()

    上述代码执行时,并没有点击[账号密码登录]的效果,并且报错

    selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

    分析源码可知,账号输入所在的div默认是不可见的,手动点击账号可使其可见。但是手动点击[账号与密码登录]不生效。

    如何定位呢?

    1.先排除是否找到了需要click的元素

    a.确实元素是否在frame,在的话,是否以成功切换了frame。

    输出window_handles可知,虽然内嵌了frame,但是实际上还是在一个window上,那么不能由此判断是否已成功切换了frame。试了下switch_to.frame不存在的frame,报错NoSuchFrameException。那么我认为我这里切换到frame是成功了的。尽管这里似乎跨域了,但是好像并不影响。

    关于跨域https://blog.csdn.net/a250758092/article/details/84026235

    b.这里find[账号与密码登录]时并没有抛异常,并且元素是可见的。其实可以认为元素已经找到了,但是为什么click()还是无效果呢?试下time.sleep(),发现也是也不行。

    c.试下其他点击

      js:

    firefox_driver.execute_script("document.getElementById('switcher_plogin').click()")

    如果也是不行,试下ActionChains

    2.是否存在页面刷新,导致元素失效,或者切换了其他窗口句柄呢?

    成功的代码实现

    firefox_driver = webdriver.Firefox()
        firefox_driver.get("https://www.weiyun.com/")
        firefox_driver.implicitly_wait(10)
    # 登录账号
        firefox_driver.switch_to.frame("qq_login_iframe")
        firefox_driver.execute_script("document.getElementById('switcher_plogin').click()")
        time.sleep(2)
        input_name = firefox_driver.find_element_by_css_selector("#u")
        input_name.clear()
        input_name.send_keys("***")
        input_password = firefox_driver.find_element_by_css_selector("#p")
        input_password.clear()
        input_password.send_keys("****")
        firefox_driver.find_element_by_css_selector("#login_button").click()
        time.sleep(3)
        firefox_driver.quit()
  • 相关阅读:
    Visual Basic 9.0 前沿播报·静态篇(二)对象初始化器和匿名类型
    Visual Basic 9.0 前沿播报内容概览
    Refactor! for VB —— VB2005的重构支持(一)
    我不知道该说什么,这里太先进了!
    Visual Basic 9.0 前沿播报·静态篇(一)局部变量类型推测和数组初始化器
    关于“就地颠倒句子里的词”面试题
    Visual Basic 9.0 前沿播报·静态篇(三)扩展方法
    《窝窝世界》视频20101025
    Silverlight 游戏开发小技巧:透明背景的Silverlight程序
    Silverlight 游戏开发小技巧:技能冷却效果2(Cooldown)2
  • 原文地址:https://www.cnblogs.com/Cc905/p/12884422.html
Copyright © 2011-2022 走看看