实现自动化登录微云账号。
问题分析
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()
看完点赞 ~养成好习惯,以上内容希望对你有帮助,如果对软件测试、接口测试、自动化测试、面试经验交流感兴趣可以加入我们。642830685,免费领取最新软件测试大厂面试资料和Python自动化、接口、框架搭建学习资料!技术大牛解惑答疑,同行一起交流。