1. 程式人生 > >java selenium (十四) 處理Iframe 中的元素

java selenium (十四) 處理Iframe 中的元素

toolbar htm 技術 方法 abs 中心 復制代碼 stat 浦東

什麽是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\\[email protected]
/* */\\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"); }
技術分享

java selenium (十四) 處理Iframe 中的元素