当前位置: 首页 > 工具软件 > Splinter > 使用案例 >

splinter iframe

夹谷野
2023-12-01

有时候一个页面可能会嵌套多个iframe,元素在iframe里时,是无法直接对其操作的,我们可以先告诉浏览器,进入到iframe里,然后才能操作元素。

例如:页面上有个iframe,想要在iframe里的任务名称里填写内容,就要先进入到iframe里。用switch_to.frame结合find_elements_by_tag_name就可以做到。

这两个是selenium的原生API。

 

# -*- coding: utf-8 -*-

from splinter.browser import Browser

import time

 

bs = Browser('chrome')

bs.visit('http://www.sterson.com.cn/test')

time.sleep(1)

bs.driver.switch_to.frame(bs.driver.find_elements_by_tag_name("iframe")[0])

bs.fill('task_name','testiframe')

bs.driver.switch_to.default_content()

 

这里用的switch_to.frame是selenium原生的

splinter也提供了一个get_iframe(**kwds)的API,但我没试成功过,可能是我装的版本是早期的,官方说明这个方法,可以传入iframe的name,id,或是下标。

 

当操作完后,通常还要退出当前iframe,返回到父级

switch_to.default_content()

更多自动化测试资料欢迎浏览李老道自学网:http://www.sterson.com.cn

 类似资料:

相关阅读

相关文章

相关问答