• 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()
  • 相关阅读:
    破圈法求最小生成树+最小生成树与最短路径问题
    相信你们的锐意前行,终将成就更美好的未来
    “基于模块化自动驾驶底盘的PIX移动空间”发布
    Node:通过 Uglify 压缩小程序代码
    CSS 实用技巧
    websercie调用方式
    IDEA 添加tomcat启动后控制台乱码解决方案
    对象转json串,json转对象
    js 判断一个变量是数组还是对象
    @Transactional 锁表吗?关于Spring注解@Transactional和SQL for update 的一些观点
  • 原文地址:https://www.cnblogs.com/Cc905/p/12884422.html
Copyright © 2020-2023  润新知