26.定位frame中的元素

场景

处理frame需要用到2个方法,分别是switch_to_frame(name_or_id_or_frame_element)和switch_to_default_content()

如何理解这个switch_to_frame(name_or_id_or_frame_element)方法呢?可以简单记忆一下,如果这个frame有name和id属性那么就用这两个属性就好,如果没有的话可以先用find_element_by_xxx方法找到这个frame元素,然后把这个元素传进去,这也是可行的。

switch_to_frame方法把当前定位的主体切换了frame里。怎么理解这句话呢?我们可以从frame的实质去理解。frame中实际上是嵌入了另一个页面,而webdriver每次只能在一个页面识别,因此才需要用switch_to_frame方法去获取frame中嵌入的页面,对那个页面里的元素进行定位。

switch_to_default_content方法的话则是从frame中嵌入的页面里跳出,跳回到最外面的原始页面中。

如果页面上只有1个frame的话那么这一切都是很好理解的,但如果页面上有多个frame,情况有稍微有点复杂了。

下面的代码中frame.html里有个id为f1的frame,而f1中又嵌入了id为f2的frame,该frame加载了百度的首页。

经验

假设页面上有A、B两个frame,其中B在A内,那么定位B中的内容则需要先到A,然后再到B。如果是定位A中的内容,那么直接switch_to_frame(‘A‘)就可以了;

switch_to.frame的参数问题。官方说name是可以的,但是经过实验发现id也可以。所以只要frame中id和name,那么处理起来是比较容易的。如果frame没有这两个属性的话,你可以直接加上,这对整个页面影响不大;

页面中使用frame会影响页面渲染速度,如果你遇到页面中有多个frame的情况,你完全可以提出1个页面前端性能的缺陷;

如果实在搞不定页面上的frame,送你一句歌词:也许放弃才能靠近你。那么及时放弃跟此frame相关的用例才是明智之举;

frame.html

  <html>
    <head>
      <meta http-equiv="content-type" content="text/html;charset=utf-8" />
      <title>frame</title>
      <script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
      <script type="text/javascript">
        $(document).ready(function(){
        });
      </script>
    </head>

    <body>
      <div class="row-fluid">
        <div class="span10 well">
          <h3>frame</h3>
          <iframe id="f1" src="inner.html" width="800", height="600"></iframe>
        </div>
      </div>
    </body>
    <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
  </html>

inner.html

  <html>
    <head>
      <meta http-equiv="content-type" content="text/html;charset=utf-8" />
      <title>inner</title>
    </head>

    <body>
      <div class="row-fluid">
        <div class="span6 well">
          <h3>inner</h3>
          <iframe id="f2" src="http://www.baidu.com" width="700" height="500"></iframe>
          <a href="javascript:alert(‘watir-webdriver better than selenium webdriver;‘)">click</a>
        </div>
      </div>
    </body>
  </html>

注:frame.html和inner.html和test.py放在同一个文件夹下

创建test.py输入一下代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import os

if ‘HTTP_PROXY‘ in os.environ: del os.environ[‘HTTP_PROXY‘]

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

#先到f1再到f2
dr.switch_to_frame(‘f1‘)
dr.switch_to_frame(‘f2‘)

#往f2中的百度关键字文本框中输入内容
dr.find_element_by_id(‘kw‘).send_keys(‘watir-webdriver‘)

#直接跳出所有frame
dr.switch_to_default_content()

#再到f1
dr.switch_to_frame(‘f1‘)
dr.find_element_by_link_text(‘click‘).click()

sleep(2)
dr.quit()

原文地址:https://www.cnblogs.com/luoshuifusheng/p/9183286.html

时间: 2024-08-24 06:44:58

26.定位frame中的元素的相关文章

10. 定位frame中的元素

场景 处理frame需要用到2个方法,分别是switch_to_frame(name_or_id_or_frame_element)和switch_to_default_content() 如何理解这个switch_to_frame(name_or_id_or_frame_element)方法呢?可以简单记忆一下,如果这个 frame有name和id属性那么就用这两个属性就好,如果没有的话可以先用find_element_by_xxx方法找到这个frame元素,然后 把这个元素传进去,这也是可行

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element 定位frame中的元素

from selenium import webdriver import time def sleep(w=1): time.sleep(w) return 0 # 初始化浏览器信息 driver = webdriver.Chrome() driver.get("http://m.mail.10086.cn") print("------------------------------login in-------------------------------"

定位frame中的元素

场景 处理frame需要用到2个方法,分别是switch_to_frame(name_or_id_or_frame_element)和switch_to_default_content() 如何理解这个switch_to_frame(name_or_id_or_frame_element)方法呢?可以简单记忆一下,如果这个frame有name和id属性那么就用这两个属性就好,如果没有的话可以先用find_element_by_xxx方法找到这个frame元素,然后把这个元素传进去,这也是可行的.

定位 frame 中的对象

1.脚本准备 frame.html 中嵌套 inner.html ,两个文件和我们的脚本文件放同一个目录下. frome.html代码如下: <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>frame</title> <script type="text/jav

转:python webdriver API 之定位 frame 中的对象

在 web 应用中经常会出现 frame 嵌套的应用,假设页面上有 A.B 两个 frame,其中 B 在 A 内,那么定位 B 中的内容则需要先到 A,然后再到 B.switch_to_frame 方法可以把当前定位的主体切换了 frame 里.怎么理解这句话呢?我们可以从 frame的实质去理解.frame 中实际上是嵌入了另一个页面,而 webdriver 每次只能在一个页面识别,因此才需要用 switch_to.frame 方法去获取 frame 中嵌入的页面,对那个页面里的元素进行定位

使用三种方式定位html中的元素

1)使用三种方式定位html中的元素a)通过ID$("#ID")b)通过标签名$("标签名")c)通过样式名$(".样式名")2)dom中,需要判段查找到的元素是否为null,而jquery中,无需判段,因为jquery本身内置判段器,在查找不到的情况下,返回"undefined" <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN&quo

【selenium自动化——定位frame中的对象】

页面上有 A.B 两个 frame,其中 B 在 A 内,那么定位 B 中的内容则需要先到 A,然后再到 B. switch_to_frame 方法可以把当前定位的主体切换到 frame 里 代码示例: from selenium import webdriverimport time...... driver.implicitly_wait(30)#先找到到 ifrome1(id = f1)driver.switch_to_frame("f1")#再找到其下面的 ifrome2(id

selenium python (八)定位frame中的对象

#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip'#在测试过程中经常遇到frame嵌套的应用,加入页面上有A.B两个frame,B在A内,那么要找到B中的元素,则首先应找到A然后再到B.可以通过switch_to_frame from selenium import webdriverimport  os #打开页面driver = webdriver.Firefox()file_path = 'file:///' +

Java中通过Selenium WebDriver定位iframe中的元素

问题:有一些元素,无论是通过id或是xpath等等,怎么都定位不到. 分析:这很可能是因为你要定位的元素被嵌套在了当前页面的一个iframe元素中,Selenium对iframe中的元素有特殊的定位规则,WebDriver不能够直接进行定位. 解决办法:我们要把这个iframe元素找出来,让WebDriver转移到这个iframe元素上,之后再让WebDriver对iframe中的元素进行定位. 因为最近在用Java来做一些东西,所以就顺便说一下Selenium在Java环境下的使用,总共分三步