Selenium webdriver定位iframe里面元素两种方法
以东方财富网登录页面为例:

在查找元素过程中,直接通过id或者xpath等找不到元素,查看页面源代码发现元素是属于iframe里,例如:
<div class="wrap_login">
<iframe class="frame_login" src="https://exaccount.eastmoney.com/home/login?request=%7b%22agentPageUrl%22%3a%22https%3a%2f%2fpassport.eastmoney.com%2fpub%2fLoginAgent%22%2c%22redirectUrl%22%3a%22http%3a%2f%2fwww.eastmoney.com%2f%22%2c%22callBack%22%3a%22LoginCallBack%22%2c%22redirectFunc%22%3a%22PageRedirect%22%2c%22data%22%3a%7b%22domainName%22%3a%22passport.eastmoney.com%22%2c%22deviceType%22%3a%22Web%22%2c%22productType%22%3a%22UserPassport%22%2c%22versionId%22%3a%220.0.1%22%7d%7d"></iframe>
</div>
以下为了定位到iframe里面元素,有2种方法:
方法一:单独打开iframe网址,直接定位。

@Test
public void loginTest() throws InterruptedException { System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://exaccount.eastmoney.com/home/login?request=%7b%22agentPageUrl%22%3a%22https%3a%2f%2fpassport.eastmoney.com%2fpub%2fLoginAgent%22%2c%22redirectUrl%22%3a%22http%3a%2f%2fwww.eastmoney.com%2f%22%2c%22callBack%22%3a%22LoginCallBack%22%2c%22redirectFunc%22%3a%22PageRedirect%22%2c%22data%22%3a%7b%22domainName%22%3a%22passport.eastmoney.com%22%2c%22deviceType%22%3a%22Web%22%2c%22productType%22%3a%22UserPassport%22%2c%22versionId%22%3a%220.0.1%22%7d%7d");
driver.manage().window().maximize();
Thread.sleep(3000);
driver.findElement(By.id("txt_account")).sendKeys("ycyzharry");
driver.findElement(By.id("txt_pwd")).sendKeys("password");
driver.findElement(By.id("btn_login")).click();
} }
方法二:先切换到iframe,再进行定位。
@Test
public void clickButtonTest() throws InterruptedException { System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://passport.eastmoney.com/pub/login?backurl=http%3A//www.eastmoney.com/");
driver.manage().window().maximize();
Thread.sleep(3000); WebElement iframe = driver.findElement(By.className("frame_login"));
driver.switchTo().frame(iframe);
driver.findElement(By.id("txt_account")).sendKeys("ycyzharry");
driver.findElement(By.id("txt_pwd")).sendKeys("password");
driver.findElement(By.id("btn_login")).click();
} }
值得注意的是,如果iframe有id时,切换iframe时把id作为参数直接传入
driver.switchTo().frame("id");
以上示例中,iframe没有id,只有class="frame_login",所以我们需要先通过classname定位到iframe元素,再把该元素作为参数传入。
例如,如果要跳出iframe,可以使用以下方法:
driver.switchTo().defaultContent();
Selenium webdriver定位iframe里面元素两种方法的更多相关文章
- Selenium webdriver定位iframe里面元素
在查找元素过程中,直接通过id或者xpath等找不到元素,查看页面源代码发现元素是属于iframe里,例如: <div class="wrap_login"> < ...
- Java中通过Selenium WebDriver定位iframe中的元素
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 问题:有一些元素,无论是通过id或是xpath等等,怎么都定位不到. 分析:这很可能是因为你要定位 ...
- 转载:selenium webdriver定位不到元素的五种原因及解决办法
1.动态id定位不到元素for example: //WebElement xiexin_element = driver.findElement(By.id("_mail_c ...
- selenium webdriver定位不到元素的五种原因及解决办法
1.动态id定位不到元素 for example: //WebElement xiexin_element = driver.findElement(By.id("_mail_ ...
- [Python爬虫] 之七:selenium webdriver定位不到元素的五种原因及解决办法(转载)
转载:http://www.51testing.com/html/87/300987-831171.html 1.动态id定位不到元素for example: //WebElement ...
- web页面中快速找到html对应元素两种方法
一.第一种方法(通过先进入开发模式然后再去选择网页元素) 1.打开IE.Chrome.FireFox等,按 F12 键进入开发模式 2.在打开的控制窗口左上角有个 箭头 按钮,点击它之后,此时将鼠标 ...
- selenium webdriver学习--------iframe的处理
有时候我们在定位一个页面元素的时候发现一直定位不了,反复检查自己写的定位器没有任何问题,代 码也没有任何问题.这时你就要看一下这个页面元素是否在一个iframe中,这可能就是找不到的原因之一.如果你在 ...
- jquery阻止元素冒泡的两种方法
通常情况下,如果给父元素添加事件之后,子元素也会继承同样的事件,这个时候就要阻止子元素的这种行为,成为阻止冒泡,总结两种解决方法: html代码: <div id="parent&qu ...
- JS动态创建元素(两种方法)
前言 创建元素有两种方法 1)将需要创建的元素,以字符串的形式拼接:找到父级元素,直接对父级元素的innnerHTML进行赋值. 2)使用Document.Element对象自带的一些函数,来实现动态 ...
随机推荐
- javaSE_08Java中static、继承、重写-思维导图
思维导图看不清楚时: 1)可以将图片另存为图片,保存在本地来查看 : 2)右击在新标签中打开放大查看 (IE不支持,搜狗,360可以):
- 543. Diameter of Binary Tree
https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to ...
- Python进制转换(二进制、十进制和十六进制)
#!/usr/bin/env python # -*- coding: utf-8 -*- # 2/10/16 base trans. wrote by srcdog on 20th, April, ...
- ue4打包问题的巧妙解决——二分回退大法!
昨天突然发生了一件非常恐怖的事--我的ue4项目居然不能打包了!! 大概是这么一回事: UATHelper: 打包 (Windows (64位)): UnrealBuildTool: ERROR: ...
- 【论文:麦克风阵列增强】Speech Enhancement Based on the General Transfer Function GSC and Postfiltering
作者:桂. 时间:2017-06-06 16:10:47 链接:http://www.cnblogs.com/xingshansi/p/6951494.html 原文链接:http://pan.ba ...
- easygen通用代码生成框架[开源]
什么东东 用过mybatis的同学都知道,手工写mapper和xml是一件很痛苦的事儿,幸好官方提供了Mybatis-Generator,但是这家伙生成的东西不开放不方便修改,而且项目中的代码生成需求 ...
- react-router 参数获取
No BB!!! show me the code. Main.js import { BrowserRouter, Route, Link ,Switch} from 'react-router-d ...
- ntopng-一款流量审计框架的安装以及应用
核心交换机镜像流量审计对于企业应急响应和防患于未然至关重要,本文想通过介绍ntopng抛砖引玉讲一讲流量审计的功能和应用. 安装 安装依赖环境: sudo yum install subversion ...
- tomcat抬头有“选择”或“选定”,导致tomcat无法运行问题
2. 遇到tomcat抬头有"选择"或"选定",导致tomcat无法运行问题 解决:在tomcat抬头右键--属性,去掉"快速编辑模式"勾选 ...
- Xamarin开发笔记—设备类&第三方弹窗的使用和注意事项
一.设备类是Xamarin重要开发组成部分,下面介绍一下设备类的主要用法: //唤醒打电话 Device.OpenUri(new Uri("tel:180xxxxxxxx")); ...