一. driver.switch_to.frame(id):可以通过id切换到iframe

之前学习了selenium切换到iframe的方法,代码如下

from selenium import webdriver
driver = webdriver.Chrome()

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对象来定位

源码中并没有说可以通过id定位,但事实上是可以的

二. 嵌套iframe的切换

然而,对于嵌套的iframe,又该如何解决:

1. index定位行不通

当时第一点想到的是通过index定位,即第一个iframe为0,第二个为1

 __author__ = '小翟'

 from selenium import webdriver
 import time
 import os

 driver = webdriver.Chrome()
 file_path = "file:///" + os.path.abspath("frame.html")
 driver.get(file_path)

 #窗口最大化
 driver.maximize_window()

 #隐性等待30s
 driver.implicitly_wait(30)
 #先找到iframe1(id="f1")
 driver.switch_to.frame(0)
 #再找到其下面的iframe2(id="f2")
 driver.switch_to.frame(1)

 #下面就可以正常的操作元素了
 driver.find_element_by_id("kw").send_keys("selenium")
 driver.find_element_by_id("su").click()

 time.sleep(2)

 driver.quit()

运行结果报错了,显示的第19行出错,找不到下标为1的frame

Traceback (most recent call last):
  File "D:/python_workshop/python6/selenium_webdriver/多层框架定位.py", line 19, in <module>
    driver.switch_to.frame(1)
  File "D:\Program\python34\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 89, in frame
    self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
  File "D:\Program\python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "D:\Program\python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchFrameException: Message: no such frame
  (Session info: chrome=66.0.3359.139)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.1.7600 x86_64)

一定要注意,对于并列的iframe,我们可以用下标表示,但是嵌套的,除了第一个可以用下标0表示外,其他都不行

#并列的iframe

iframe ---------------------------下标0
   |
   |
   |
iframe----------------------------下标1
   |
   |
   |
iframe----------------------------下标2
#嵌套的iframe

iframe -----------------------------------id="f1"

    iframe ------------------------------------id="f2"

            iframe -------------------------------------id="f3"

2. 通过id定位

#先找到iframe1(id="f1")
driver.switch_to.frame("f1")
#再找到其下面的iframe2(id="f2")
driver.switch_to.frame("f2")

完美解决,还有一个问题,我们要向从切到上级iframe,应该如何实现

3. 从子iframe切到父iframe

selenium提供了一个类似"后退"的方法,如下

driver.switch_to.parent_frame()  # 如果当前已是主文档,则无效果

三. 总结

对于iframe处理,目前主要有三种方法

 driver.switch_to.frame(reference)
 driver.switch_to.parent_frame()
 driver.switch_to.default_content()

其中第一种iframe的切换的参数有4个: idex, id, name, webelement

参考文章

https://blog.csdn.net/huilan_same/article/details/52200586

selenium定位多个嵌套iframe的更多相关文章

  1. Selenium定位不到指定元素原因之iframe(unable to locate element)

    浏览过程中,图片中的内容可能太小,无法看清,可以>右键>在新标签中打开 Outline 项目原因,需要用selenium实现模拟登陆.模拟上传文件,自然就需要模拟点击[上传]按钮: 模拟点 ...

  2. Selenium踩坑记之iFrame的定位与切换

    转自:https://www.jianshu.com/p/6e7d0359e4bb Selenium是浏览器自动化测试的工具之一,用过的人都懂他的好,也被他坑的不要不要的.今天就聊聊Selenium的 ...

  3. selenium元素定位不到之iframe

    我们在使用selenium的18中定位方式的时候,有时会遇到定位不上的问题,今天我们就来说说导致定位不上的其中一个原因---iframe 问题描述:通过firebug查询到相应元素的id或name等, ...

  4. Selenium定位iframe动态ID

    Selenium定位iframe动态ID. 126邮箱实例 买了本虫师的书来学习selenium2自动化测试,然后写第一个实例就遇到了一些坑,好在有热心的网友提供了帮助,解决了问题 要学习seleni ...

  5. python+selenium实现163邮箱登陆—iframe动态ID定位 及常用定位方法

    今天发现之前的登录163邮箱脚本定位不到iframe了,原因是iframe拼接了动态ID,修改后的脚本如下: from selenium import webdriver driver = webdr ...

  6. Selenium 多表单(frame/iframe)切换

    frame标签有frameset.frame.iframe三种,frameset跟其他普通标签没有区别,不会影响到正常的定位,而frame与iframe需要切换进去才能定位到其中的元素 比如下面这个网 ...

  7. Python3 Selenium定位不到元素常见原因及解决办法

    Python3 Selenium定位不到元素常见原因及解决办法 一.问题描述 在做web应用的自动化测试时,定位元素是必不可少的,这个过程经常会碰到定位不到元素的情况: 报错信息: no such e ...

  8. No.4 selenium学习之路之iframe

    查看iframe: 1.top window ——可以直接进行定位

  9. WebDriver中如何处理Iframe 及 嵌套Iframe

    最近在用webdriver进行爬虫的时候,遇到了网站存在iframe的情况,处理了好久没有解决,后来发现原来webdriver自带处理方法,汗颜.. 1.iFrame有ID 或者 name的情况 // ...

随机推荐

  1. .Net自带ChartControl报错:Auto interval does not have proper value

    出现这个错误的原因是我们给ChartControl同时设置了Minimum和Maxmum的值,而这两个值又恰好相等. chart.ChartAreas[0].AxisY.Minimum=min; ch ...

  2. Springboot入门2-配置druid

    Druid是Java语言中最好的数据库连接池,在连接池之外,还提供了非常优秀的监控功能. 下面来说明如何在 Spring Boot 中配置使用Druid 1.添加Maven依赖 (或jar包) < ...

  3. 3.6.使用STC89C52控制MC20解析GPS的经纬度数据上传到指定服务器

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  4. 剑指offer 面试63题

    面试63题 题目:股票的最大利润 题:假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可获得的最大利润是多少?例如,一只股票在某些时间节点的价格为{9,11,8,5,7,12,16, ...

  5. mssql 中文乱码 字库集 问题解决方法

    The database could not be exclusively locked to perform the operation(SQL Server 5030错误解决办法)   SQL S ...

  6. dom树改变监听

    function unwrap(el, target) { if ( !target ) { target = el.parentNode; } while (el.firstChild) { tar ...

  7. 预防SQL注入攻击

    /** * 预防SQL注入攻击 * @param string $value * @return string */ function check_input($value) { // 去除斜杠 if ...

  8. Python学习进程(2)Python环境的搭建

        本节主要介绍在windows和Linux平台上如何搭建Python编程环境.     (1)查看Python版本: windows: C:\Users\JMSun>python 'pyt ...

  9. SQL 根据IF判断,SET字段值

    当INVOICE_STATUS值为1时,赋值为2,否者赋值为原来的值 UPDATE T_INVOICE SET DOWNLOAD_COUNT = DOWNLOAD_COUNT + 1, INVOICE ...

  10. 【HackerRank】Sherlock and MiniMax

    题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer  ...