网站首页 全球最实用的IT互联网站!

人工智能P2P分享Wind搜索发布信息网站地图标签大全

当前位置:诺佳网 > 软件工程 > 其他技术区 > 软件测试 >

selenium之 frame、iframe

时间:2025-03-26 16:14

人气:

作者:admin

标签:

导读:frame、iframe 相信大部分使用selenium的同学都会遇见一个现象,就是明明可以定位到元素,但是就是无法操作;这个是因为遇到了frame、iframe这个东西 frame标签有frameset、frame、iframe三种,...

frame、iframe

相信大部分使用selenium的同学都会遇见一个现象,就是明明可以定位到元素,但是就是无法操作;这个是因为遇到了frame、iframe这个东西

frame标签有frameset、frame、iframe三种,

frameset和其他普通标签没有区别,不会影响到定位

它们两个的学名叫做框架,顾名思义,首先你要进去这个框架再进行操作,操作完成再从这个框架离开

selenium就提供了三种方式,来进行frame、iframe的访问

  • WebElement
  • name、id
  • 使用索引

话不多说,让我们开始实践

WebElement

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

serve_path = r'D:\Code_Study\driver\chromedriver-win64\chromedriver.exe'
service = Service(serve_path)
browser = webdriver.Chrome(service=service)
browser.get(f'https://the-internet.herokuapp.com/nested_frames')
# WebElement;先找到这个框架元素,在进行切换
frameset = browser.find_element(By.NAME, "frame-top")
# 切换到大的frame
browser.switch_to.frame(frameset)
# 定位上半部分中间的frame,并切换
frame_middle = browser.find_element(By.NAME, "frame-middle")
browser.switch_to.frame(frame_middle)
# 输出里面的内容
print(browser.find_element(By.ID, "content").text) # MIDDLE

name、id

# 如果frama有Id、name属性的话,可以使用该属性;如果不是唯一的话,会默认找第一个
# 先找到上面的frame,再找到中间的
browser.switch_to.frame("frame-top")
browser.switch_to.frame("frame-left")
# # 输出里面的内容
print(browser.find_element(By.TAG_NAME, "body").text) # LEFT

使用索引

frame_top = browser.find_elements(By.TAG_NAME, 'frame')[0]
browser.switch_to.frame(frame_top)
frame_right = browser.find_elements(By.TAG_NAME, 'frame')[2]
browser.switch_to.frame(frame_right)
print(browser.find_element(By.TAG_NAME, "body").text) # RIGHT

注意离开框架

不管你使用上面什么方法,最后需要离开框架,切换回默认内容

  • switch_to.parent_frame()返回父文档
  • switch_to.default_content()返回主文档
# 离开框架
# switch_to.default_content()返回主文档
# switch_to.parent_frame()返回父文档

frame_top = browser.find_elements(By.TAG_NAME, 'frame')[0]
browser.switch_to.frame(frame_top)
frame_right = browser.find_elements(By.TAG_NAME, 'frame')[2]
browser.switch_to.frame(frame_right)
print(browser.find_element(By.TAG_NAME, "body").text) # RIGHT
# 返回父文档,回到frame_top
# browser.switch_to.parent_frame()
# print(browser.page_source)
"""
<html><head></head><frameset frameborder="1" name="frameset-middle" cols="33%,33%,33%">
  <frame src="/frame_left" scrolling="no" name="frame-left">
  <frame src="/frame_middle" scrolling="no" name="frame-middle">
  <frame src="/frame_right" scrolling="no" name="frame-right" cd_frame_id_="757301613216e105ebb32711cd5c35dd">
</frameset>
</html>
"""


# 直接返回主文档
browser.switch_to.default_content()
print(browser.page_source)
"""
<html><head></head><frameset frameborder="1" rows="50%,50%">
  <frame src="/frame_top" scrolling="no" name="frame-top" cd_frame_id_="a74a433876cf3abadc9fddf601c55829">
  <frame src="/frame_bottom" scrolling="no" name="frame-bottom">
  <noframes>
    Frames are not rendering.
  </noframes>
</frameset>
</html>
"""

好了,frame的东西到这里就结束了;有疑问的可以评论区讨论

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信