有时候我们定位元素的时候,发现怎么都定位不了。 这时候你需要查一查你要定位的元素是否在iframe里面

 

阅读目录

 

 

什么是iframe

iframe 就是HTML 中,用于网页嵌套网页的。 一个网页可以嵌套到另一个网页中,可以嵌套很多层。

 

selenium 中提供了进入iframe 的方法

 

// 进入 id 叫frameA 的 iframe
dr.switchTo().frame("frameA");
// 回到主窗口 dr.switchTo().defaultContent();

 

main.html

<html>
<head>
    <title>FrameTest</title>
</head>
<body>
    <div id="id1">this is main page's div!</div>
    <input type="text" id="maininput" />
    <br/>
    <iframe id="frameA" frameborder="0" scrolling="no" style="left:0;position:absolute;" src="frame.html"></iframe>
</body>
</html>  

frame.html

<html>
<head>
    <title>this is a frame!</title>
</head>
<body>
    <div id="div1">this is iframes div,</div>
    <input id="iframeinput"></input>
</body>
</html>  

 

selenium 代码

    public static void testIframe(WebDriver driver)
    {
        driver.get("E:\\StashFolder\\huoli_28@hotmail.com\\Stash\\Tank-MoneyProject\\浦东软件园培训中心\\我的教材\\Selenium Webdriver\\frame\\main.html");    
        
        // 在 主窗口的时候
        driver.findElement(By.id("maininput")).sendKeys("main input");
        // 此时 没有进入到iframe, 以下语句会报错
        //driver.findElement(By.id("iframeinput")).sendKeys("iframe input");
                
        driver.switchTo().frame("frameA");
        driver.findElement(By.id("iframeinput")).sendKeys("iframe input");
        
        // 此时没有在主窗口,下面语句会报错
        //driver.findElement(By.id("maininput")).sendKeys("main input");
        
        // 回到主窗口
        driver.switchTo().defaultContent();
        driver.findElement(By.id("maininput")).sendKeys("main input");  
    }

 

 

 

 

 

 

posted on 2016-03-12 19:51  小坦克  阅读(17263)  评论(0编辑  收藏  举报