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对象自带的一些函数,来实现动态 ...
随机推荐
- redis集群搭建实践
参考 第一个节点 第一个节点为本地的机器 IP:192.168.23.148 检查机器配置 $ uname -a Linux wangya-Lenovo-G480 4.8.0-52-generic # ...
- 主存与Cache的地址映射
最近在复习计算机体系结构,选用的教材是名闻遐迩的<计算机体系结构 量化研究方法 第五版>(Computer Architecture A Quantitative Approach), 关 ...
- [翻译]怎么写一个React组件库(二)
本文同步发布于知乎专栏 https://zhuanlan.zhihu.com/p/27434018,喜欢本文的就去知乎点个赞支持下吧- 引言 该系列文章将通过创建一个组件库来引导你学习如何构建自己的组 ...
- js数组及数组应用(冒泡和二分,遍历输出)
一.定义:1)var arr=new Array(); 加数据:arr[0]=1; 2)定义同时赋值:var arr=new Array(1,2,3,4,5); 3)调用:var arr=new Ar ...
- HttpWebRequest操作已超时
最近我们使用.NET3.5HttpWebRequest会报操作已超时但使用.NET4.0版本及以上却可以正常访问. 一段简单的代码如下: string returnData = "" ...
- R语言统计分析技术研究 特征值选择技术要点
特征值选择技术要点 作者:王立敏 文章来源: 网络 1.特征值 特征值是线性代数中的一个重要概念.在数学,物理学,化学,计算机等领域有着广泛的应用. ...
- Redis中的基本数据结构
Redis基础数据结构 基础数据结构 sds简单动态字符串 数据结构 typedef struct sdstr{ int len // 字符串分配的字节 int free // 未使用的字节数 cha ...
- Bash On Windows的学习
Bash On Windows的学习 Bash On Windows的卸载 删除软件和设置:在 cmd 运行lxrun /uninstall 删除所有文件:在cmd中运行lxrun /uninstal ...
- Linux编程之select
select系统调用的的用途是:在一段指定的时间内,监听用户感兴趣的文件描述符上可读.可写和异常等事件. select 机制的优势 为什么会出现select模型? 先看一下下面的这句代码: int i ...
- PHP把2个二维数组合并一个二维数组
$a = array(0 => Array(id => 66,class_name => www.iiwnet.com),1 => Array(id => 67,clas ...