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()
  • 相关阅读:
    LeetCode: Search in Rotated Sorted Array
    LeetCode: Search in Rotated Sorted Array II
    LeetCode: Search Insert Position
    LeetCode: Same Tree
    LeetCode: Search a 2D Matrix
    Oracle工具下载地址
    华为创始人任正非简介
    个人开公司的流程,以后用得着!
    如何更改收藏夹的位置,收藏夹的位置
    帮助大家了解到Google关键字排名、价格
  • 原文地址:https://www.cnblogs.com/Cc905/p/12884422.html
Copyright © 2011-2022 走看看