自动化笔记-iframe

来源:互联网 发布:苹果 淘宝 编辑:程序博客网 时间:2024/06/05 08:29

     当我们在定位页面元素,尝试多种定位方法,且明明运用id、name等常用定位方法无误,但是还是无法定位到元素时,我们就第一个反应就是该元素是不是嵌套在iFramen内

如果是,我们就需要先定位iframe,再定位里边的元素。

    具体例子:


1.新建2个html页面,放于D盘,作为被测试的页面


main.html


<html>
    <head >
        <title> FrameTest</title >
    </head >
    <body >
        <div id = "id1"> this is a div !</ div>
        <iframe id = "frame"  frameborder="0" scrolling="no" style="left :0; position:absolute;" src = "iframe.html"></ iframe>
    </body >
</html><span style="font-family: Tahoma;">&nbsp;</span>


iframe.html

<html>
    <head >
        <title> this is a frame!</title >
    </head >
    <body >
        <div id = "div1"> this is a div !</div>
        <label> input:</label >
        <input id = "input1"></ input>
    </body >
</html>


2.具体代码

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class IFrameTest {
    @Test
    public void iFrameTest() throws InterruptedException {
        // 设置chromedriver的路径,根据你具体存放位置来设置路径
        System.setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe");
        // 启动Chrome浏览器
        WebDriver driver = new ChromeDriver();
        // get方式打开测试页面
        driver.get("C:\\main.html");
        // 选取frame
        driver.switchTo().frame("frame");;
        // 定位iframe里面的文本框
        driver.findElement(By.id("input1")).sendKeys("这是在iframe里面的文本框");
        // 跳出iframe
        driver.switchTo().defaultContent();
        // 为了看效果,等待3S
        Thread.sleep(3000);
        // 结束测试
        driver.quit();
    }
}






0 0