selenium.common.exceptions.NoSuchFrameException: Message:xxx 谁借莪1个温暖的怀抱¢ 2022-11-13 04:24 111阅读 0赞 首先描述一下问题: 自动化QQ邮箱登录,账户密码输入后跳转到验证界面,此时需要定位元素却怎么也定位不到: 账户密码输入有问题的请参考:[无法定位元素的问题][Link 1] 代码如下: browser = webdriver.Firefox() browser.get("https://mail.qq.com/") browser.switch_to.frame("login_frame") browser.find_element_by_class_name("inputstyle").clear() browser.find_element_by_class_name("inputstyle").send_keys("xxxx") browser.find_element_by_class_name("inputstyle.password").clear() browser.find_element_by_class_name("inputstyle.password").send_keys("xxxx") browser.find_element_by_id("login_button").click() browser.find_element_by_class_name("login_button").click() # browser.switch_to.default_content() # 回到默认的iframe # 等待资源加载 time.sleep(3) # 等待图片加载出来 browser.find_element_by_id('tcaptcha_drag_button').click() 页面元素如下: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdWxhbmJh_size_16_color_FFFFFF_t_70]但是报错:NoSuchElementException 再仔细查找一下,发现最靠近该元素的iframe是另一个 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdWxhbmJh_size_16_color_FFFFFF_t_70 1] 修改代码,先回到默认iframe,再到新的iframe: browser = webdriver.Firefox() browser.get("https://mail.qq.com/") browser.switch_to.frame("login_frame") browser.find_element_by_class_name("inputstyle").clear() browser.find_element_by_class_name("inputstyle").send_keys("xxxx") browser.find_element_by_class_name("inputstyle.password").clear() browser.find_element_by_class_name("inputstyle.password").send_keys("xxxx") browser.find_element_by_id("login_button").click() browser.find_element_by_class_name("login_button").click() browser.switch_to.default_content() # 回到默认的iframe # 等待资源加载 time.sleep(3) browser.switch_to.frame("tcaptcha_iframe") browser.find_element_by_id('tcaptcha_drag_button').click() 就会报错:NoSuchFrameException 这会都要炸掉了!歇一会硬着头皮查一下iframe: 首先看一下iframe: iframe 标签规定了一个内联框架。 一个内联框架被用来在当前 HTML 文档中嵌入另一个文档。 **平行嵌套**: ![!\[在这里插入图片描述\](https://img-blog.csdnimg.cn/20210325171205985.png?x-oss-process=image/watermark,type\_ZmFuZ3poZW5naGVpdGk,shadow\_10,text\_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdWxhbmJh,size\_16,color\_FFFFFF,t\_][https_img-blog.csdnimg.cn_20210325171205985.png_x-oss-process_image_watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdWxhbmJh_size_16_color_FFFFFF_t] **层级嵌套**: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdWxhbmJh_size_16_color_FFFFFF_t_70 2] 这一看就明白了:tcaptcha\_iframe嵌套在login\_frame中,因此上面的代码不用回到默认iframe,直接再进一层就好了: browser = webdriver.Firefox() browser.get("https://mail.qq.com/") browser.switch_to.frame("login_frame") browser.find_element_by_class_name("inputstyle").clear() browser.find_element_by_class_name("inputstyle").send_keys("xxxx") browser.find_element_by_class_name("inputstyle.password").clear() browser.find_element_by_class_name("inputstyle.password").send_keys("xxxx") browser.find_element_by_id("login_button").click() browser.find_element_by_class_name("login_button").click() #browser.switch_to.default_content() # 回到默认的iframe # 等待资源加载 time.sleep(3) browser.switch_to.frame("tcaptcha_iframe") browser.find_element_by_id('tcaptcha_drag_button').click() ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdWxhbmJh_size_16_color_FFFFFF_t_70 3]运行成功!开始下一步! [Link 1]: https://blog.csdn.net/liulanba/article/details/115203192 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdWxhbmJh_size_16_color_FFFFFF_t_70]: /images/20221022/fc4a34f17f2e4bbdb8003f4e6e191b3a.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdWxhbmJh_size_16_color_FFFFFF_t_70 1]: /images/20221022/724f92b0977d4947ab347eb7ff13d078.png [https_img-blog.csdnimg.cn_20210325171205985.png_x-oss-process_image_watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdWxhbmJh_size_16_color_FFFFFF_t]: /images/20221022/352e67dcae984095bade761e58c3ef22.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdWxhbmJh_size_16_color_FFFFFF_t_70 2]: /images/20221022/146b3764d9a54b8ba6aac711b0539a09.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpdWxhbmJh_size_16_color_FFFFFF_t_70 3]: /images/20221022/a410a362baf240fd9266154444bcd047.png
还没有评论,来说两句吧...