selenium里的iframe 报错 NoSuchFrameException: Message: no such frame: element is not a frame

本文操作iframe时遇到的报错有:

  • NoSuchFrameException: Message: no such frame: element is not a frame
  • in frame raise NoSuchFrameException(frame_reference)
    selenium.common.exceptions.NoSuchFrameException: Message: x-URS-iframe

我们都知道iframe 是HTML里面的一个用于网页嵌套网页的框架,一个网页可以嵌套到另一个网页中,可以嵌套多层,当然也可以平衡嵌套,常见的一般有两种:

  • 嵌套 框架 (大框架下包含小框架)
    在这里插入图片描述
  • 平行 嵌套 (平衡摆放,平级关系)
    在这里插入图片描述

一般定位iframe的方法

1、iframe有具体的name 和 id 属性(id和name的属性值如果是动态变化的数字 就不能使用)

.switch_to_iframe("id or name 的属性值")

2、iframe标签本身没有id 和 name 属性

  • 方式1:定位iframe元素
iframe = driver.find_elements_by_tag_name("iframe")[0]   # 根据需要填入index,这里定位HTML里的第一个
driver.switch_to.frame(iframe)
  • 方式2:用Xpath或css定位到iframe的父类
name = driver.find_element_by_xpath('//*[@id="panel"]')
driver.switch_to_iframe(name)  

3、框架有进入就会有退出 — 针对平行iframe标签

.switch_to.default_content()

下面说说,定位163邮箱遇到的一些坑

坑一:
  • 输入代码:用固定id的方法定位
# import time
# from selenium import webdriver
# driver = webdriver.Chrome()
# driver.get("https://email.163.com")
# time.sleep(50)        # 我怕不够时间加载,所以时间加长了(排除时间太短产生timeout的错误)
# driver.switch_to.default_content()
# driver.switch_to.frame('x-URS-iframe1565846475124.4539')   # 切换到框架(这里用了ID属性)
# driver.find_element_by_name("email").send_keys("bingo")
# time.sleep(1)
# driver.find_element_by_name("password").send_keys("123456")
# time.sleep(1)
# driver.find_element_by_id("dologin").click()
# driver.switch_to.default_content()  # 退出框架
  • 报错:定位不到对应的iframe
in frame raise NoSuchFrameException(frame_reference)
selenium.common.exceptions.NoSuchFrameException: Message: x-URS-iframe
  • 分析:iframe标签的id会变动
    在这里插入图片描述
  • 解决:发现这里的iframe标签的id会变动,所以不能用id属性直接定位,需要更改定位方式
坑二:
  • 输入代码:用xpath定位iframe的父类
# import time
# from selenium import webdriver
# driver = webdriver.Chrome()
# driver.get("https://email.163.com")
# time.sleep(2)
# name = driver.find_element_by_xpath('//*[@id="panel"]')     # iframe 的父类
# driver.switch_to.frame(name)
# driver.find_element_by_name("email").send_keys("bingo")
# time.sleep(1)
# driver.find_element_by_name("password").send_keys("123456")
# time.sleep(1)
# driver.find_element_by_id("dologin").click()
# driver.switch_to.default_content()  # 退出框架
  • 报错:定位到的元素不是框架
in check_response raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchFrameException: Message: no such frame: element is not a frame
  • 分析:既让定位到的是元素,那我就直接定位iframe标签了
  • 解决:
    输入代码
# import time
# from selenium import webdriver
# driver = webdriver.Chrome()
# driver.get("https://email.163.com")
# time.sleep(2)
# iframe = driver.find_elements_by_tag_name("iframe")[0]
# driver.switch_to.frame(iframe)
# driver.find_element_by_name("email").send_keys("bingo")
# time.sleep(1)
# driver.find_element_by_name("password").send_keys("123456")
# time.sleep(1)
# driver.find_element_by_id("dologin").click()
# driver.switch_to.default_content()  # 退出框架

结果:定位成功

如有不足欢迎指正,如需转载,请附上本文链接,如果觉得对你有帮助,欢迎给我一个小小的赞,谢谢!

  • 30
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
I'm sorry to hear that you're encountering a `NoSuchElementException` while using Selenium. This exception occurs when the element you're trying to locate or interact with cannot be found on the web page. To troubleshoot this issue, I suggest the following steps: 1. Verify the element locator: Double-check the correctness of the element locator (e.g., ID, class name, XPath, etc.) you are using to locate the element. Ensure that it accurately represents the element you're trying to interact with. 2. Wait for the element to appear: Sometimes, the element may take some time to load on the page. Implement an appropriate wait strategy like `WebDriverWait` to wait for the element to become visible or clickable before interacting with it. 3. Check if the element is present within an iframe: If the element you're trying to locate is within an iframe, you need to switch to that iframe context before locating the element. Use `driver.switch_to.frame()` method to switch to the correct iframe. 4. Ensure the element is not inside a shadow DOM: If the element is inside a shadow DOM, you won't be able to access it directly. You'll need to use `execute_script()` method to execute JavaScript code within the shadow DOM and perform your desired actions. 5. Verify if there are any dynamic elements: Some web pages dynamically load elements or change their structure after initial page load. If this is the case, ensure that you are locating the element after it has been fully loaded or after any dynamic changes have occurred. If none of these steps resolve the issue, providing more specific details about your code and the element you're trying to interact with would be helpful in further troubleshooting.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值