selenium测试(Java)--多表单切换

Posted qingxin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium测试(Java)--多表单切换相关的知识,希望对你有一定的参考价值。

采用下面的例子来编写用例

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>frame switch</title>
<link
    href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"
    rel="stylesheet" />
</head>
<body>
    <div class="row-fluid">
        <div class="span10 well">
            <h3>frame</h3>
            <iframe id="if" name="nf" src="http://www.baidu.com" width="800" height="300"></iframe>
        </div>
    </div>
</body>
<script
    src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script>
</html>

代码:

package com.test.frame;

import org.openqa.selenium.By;
import org.openqa.selenium.javascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class frameSwitch {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/frame/frame.html");
        driver.manage().window().maximize();

        // 利用id来切换
        waitTime(2000);
        driver.switchTo().frame("if");

        waitTime(2000);
        if (driver.findElement(By.xpath("//*[@id=\'kw\']")).isDisplayed()) {
            driver.findElement(By.xpath("//*[@id=\'kw\']")).sendKeys("switch successfully");
            // 切换到父frame
            driver.switchTo().parentFrame();
            try {
                driver.findElement(By.xpath("//*[@id=\'kw\']"));
            } catch (NoSuchElementException e) {
                String js = "alert(\\"switch to parent\\")";
                ((JavascriptExecutor) driver).executeScript(js);
                waitTime(3000);
                driver.switchTo().alert().dismiss();
            }
        }

        // 利用name来切换
        waitTime(2000);
        driver.switchTo().frame("nf");

        waitTime(2000);
        if (driver.findElement(By.xpath("//*[@id=\'kw\']")).isDisplayed()) {
            driver.findElement(By.xpath("//*[@id=\'kw\']")).sendKeys("switch successfully");
            driver.switchTo().parentFrame();
            try {
                driver.findElement(By.xpath("//*[@id=\'kw\']"));
            } catch (NoSuchElementException e) {
                String js = "alert(\\"switch to parent\\")";
                ((JavascriptExecutor) driver).executeScript(js);
                waitTime(3000);
                driver.switchTo().alert().dismiss();
            }
        }

        // 利用定位元素来切换-xpath
        waitTime(2000);
        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id=\'if\']")));

        waitTime(2000);
        if (driver.findElement(By.xpath("//*[@id=\'kw\']")).isDisplayed()) {
            driver.findElement(By.xpath("//*[@id=\'kw\']")).sendKeys("switch successfully");
            driver.switchTo().parentFrame();
            try {
                driver.findElement(By.xpath("//*[@id=\'kw\']"));
            } catch (NoSuchElementException e) {
                String js = "alert(\\"switch to parent\\")";
                ((JavascriptExecutor) driver).executeScript(js);
                waitTime(3000);
                driver.switchTo().alert().dismiss();
            }
        }

        // 利用定位元素来切换-css
        waitTime(2000);
        driver.switchTo().frame(driver.findElement(By.cssSelector("#if")));

        waitTime(2000);
        if (driver.findElement(By.xpath("//*[@id=\'kw\']")).isDisplayed()) {
            driver.findElement(By.xpath("//*[@id=\'kw\']")).sendKeys("switch successfully");
            driver.switchTo().parentFrame();
            try {
                driver.findElement(By.xpath("//*[@id=\'kw\']"));
            } catch (NoSuchElementException e) {
                String js = "alert(\\"switch to parent\\")";
                ((JavascriptExecutor) driver).executeScript(js);
                waitTime(3000);
                driver.switchTo().alert().dismiss();
                driver.quit();
            }
        }
    }

    static public void waitTime(int time) {

        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 

以上是关于selenium测试(Java)--多表单切换的主要内容,如果未能解决你的问题,请参考以下文章

Selenium 多表单(frame/iframe)切换

Selenium用法详解窗口表单切换JAVA爬虫

python+selenium2自动化---多表单多窗口切换

Python+Selenium学习笔记8 - 多表单&多窗口切换

selenium 窗口切换(多表单切换)

基于python实现UI自动化4. 一文搞定selenium 多表单(iframe/ frame)切换