Selenium学习笔记||八、Frame的处理

一、切换到frame里边

  driver.switch_to.frame(frame_reference)

  1. frame元素的name属性或ID属性

  2. 索引值(从0开始) 0 (不是下级)

  3. frame所对应的WebElement

转自:https://www.cnblogs.com/technologylife/p/5851496.html
reference是传入的参数,用来定位frame,可以传入id、name、index以及selenium的WebElement对象,假设有如下HTML代码 index.html:
<html lang="en"> <head> <title>FrameTest</title> </head> <body> <iframe src="a.html" id="frame1" name="myframe"></iframe> </body> </html> 想要定位其中的iframe并切进去,可以通过如下代码: from selenium import webdriver driver = webdriver.Firefox() driver.switch_to.frame(0) # 1.用frame的index来定位,第一个是0 # driver.switch_to.frame("frame1") # 2.用id来定位 # driver.switch_to.frame("myframe") # 3.用name来定位 # driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) # 4.用WebElement对象来定位

二、切换回主HTML里边

  driver.switch_to.default_content()

三、切换到上层

  driver.switch_to.parent_frame()

注意那些会变的ID

原文地址:https://www.cnblogs.com/Lixinhang/p/10875259.html