一、操作弹出窗口

原理 

在代码里, 通过 Set<String> allWindowsId = driver.getWindowHandles(); 

来获取到所有弹出浏览器的句柄, 然后遍历, 使用swithcto.window(newwindow_handle)方法。 就可以定位到新的窗口。

 测试页面的HTML

<html>
<head>
    <title>常见web ui元素操作, 及API使用</title>
    <script type="text/javascript">
        function open_win() 
        {
        window.open("")
        }
    </script>
</head>
<body>
 
    <form>
        <input type=button value="打开窗口" οnclick="open_win()">
    </form>
    </div>
</body>
</html>

 Java 代码

 

public static void testMultipleWindowsTitle(WebDriver driver) throws Exception
    {
        String url="E:\\StashFolder\\huoli_28@hotmail.com\\Stash\\Tank-MoneyProject\\Selenium Webdriver\\AllUIElement.html";
        driver.get(url);
        // 获取当前窗口的句柄
        String parentWindowId = driver.getWindowHandle();
        System.out.println("driver.getTitle(): " + driver.getTitle());
         
        WebElement button = driver.findElement(By.xpath("//input[@value='打开窗口']"));
        button.click();
         
        Set<String> allWindowsId = driver.getWindowHandles();
         
        // 获取所有的打开窗口的句柄
        for (String windowId : allWindowsId) {
            if (driver.switchTo().window(windowId).getTitle().contains("博客园")) {
                driver.switchTo().window(windowId);
                break;
            }
        }
         
        System.out.println("driver.getTitle(): " + driver.getTitle());
         
        // 再次切换回原来的父窗口
        driver.switchTo().window(parentWindowId);
        System.out.println("parentWindowId: " + driver.getTitle());
    }

二、智能等待页面加载完成

 我们经常会碰到用selenium操作页面上某个元素的时候, 需要等待页面加载完成后, 才能操作。 否则页面上的元素不存在,会抛出异常。

或者碰到AJAX异步加载,我们需要等待元素加载完成后, 才能操作。

 selenium 中提供了非常简单,智能的方法,来判断元素是否存在。 

实例要求 

实例:set_timeout.html 下面的html 代码, 点击click 按钮5秒后, 页面上会出现一个红色的div快, 我们需要写一段自动化脚本智能的去判断这个div是否存在, 然后把这个div 然后高亮。

<html>
    <head>
        <title>Set Timeout</title>
        <style>
            .red_box { width = 20%; height: 100px; border: none;}
        </style>
        <script>
            function show_div(){
                setTimeout("create_div()", 5000);
            }
   
            function create_div(){
                d = document.createElement('div');
                d.className = "red_box";
                document.body.appendChild(d);
            }
        </script>
    </head>
    <body>
        <button id = "b" onclick = "show_div()">click</button>
    </body>
</html>

 隐式等待 

WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/Tank/Desktop/set_timeout.html");    
 
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement element = driver.findElement(By.cssSelector(".red_box"));      
((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);

其中 

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

意思是, 总共等待10秒, 如果10秒后,元素还不存在,就会抛出异常。org.openqa.selenium.NoSuchElementException。  

显式等待 

显式等待 使用ExpectedConditions类中自带方法, 可以进行显试等待的判断。 

显式等待可以自定义等待的条件,用于更加复杂的页面等待条件。

只有满足显式等待的条件满足,测试代码才会继续向后执行后续的测试逻辑 

如果超过设定的最大显式等待时间阈值, 这测试程序会抛出异常。

public static void testWait2(WebDriver driver)
    {
        driver.get("E:\\StashFolder\\huoli_28@hotmail.com\\Stash\\Tank-MoneyProject\\浦东软件园培训中心\\我的教材\\Selenium Webdriver\\set_timeout.html");    
         
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".red_box")));
        WebElement element = driver.findElement(By.cssSelector(".red_box"));      
        ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);  
    }

三、处理 Iframe 中的元素 

有时候我们定位元素的时候,发现怎么都定位不了。 这时候你需要查一查你要定位的元素是否在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" />
     
    <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");  
    }