python+selenium四:iframe查看、定位、切换

wylc123 1年前 ⋅ 587 阅读

python+selenium四:iframe查看、定位、切换

定位iframe

1.有id,并且唯一,直接写id
driver.switch_to_frame("x-URS-iframe")
driver.switch_to.frame("x-URS-iframe")

 

2.有name,并且唯一,直接写name
driver.switch_to_frame("xxxx")
driver.switch_to.frame("xxxx")

 

3.无id,无name,先定位iframe元素
iframe = driver.find_elements_by_tag_name("iframe")[0]
driver.switch_to_frame(iframe)
driver.switch_to.frame(iframe)

 

4.通过index索引定位,(从0开始)
driver.switch_to_frame(1) # 第二个

 

使用iframe定位后需退出iframe
# 退出iframe,再操作
driver.switch_to_default_content() # 回到主页面
driver.switch_to.default_content()

 

3、多个iframe的切换问题

# 如:有两个iframe:f1、f2
# 1.定位到f1
driver.switch_to_frame("f1")
# 2.操作元素
# ······
# 3.退出iframe
driver.switch_to_default_content()
# 4.定位到f2
driver.switch_to_frame("f2")
# ······


嵌套:f1中嵌套着f2
driver.switch_to_frame("f1")
driver.switch_to_frame("f2")
# 操作元素


退出iframe:

# 第一种方式:跳出所有iframe,回到主界面
driver.switch_to_default_content()

第二种方式:回到f1(返回上一级)
driver.switch_to.parent_frame()

 

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: