zoukankan      html  css  js  c++  java
  • java selenium (十四) 处理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");  
        }
    复制代码
  • 相关阅读:
    VMware Workstation的三种网络连接方式
    sql:unix下的sql操作
    linux脚本: makefile以及链接库
    unix shell: ksh fundamental(Korn Shell)
    linux c: core dump
    linux命令:scp
    Eclipse更改默认工作目录的方法
    linux: 可重入函数与不可重入函数
    linux环境 :Linux 共享库LIBRARY_PATH, LD_LIBRARY_PATH 与ld.so.conf
    linux命令:Linux命令大全
  • 原文地址:https://www.cnblogs.com/MarchThree/p/7258123.html
Copyright © 2011-2022 走看看