Selenum2:在超级链接点击上获得相同的窗口句柄

时间:2016-04-03 05:28:38

标签: java selenium webdriver

以下index.jsp有3个超级链接(gmail,google,Facebook,java教程),即使单击超链接后我也会得到相同的窗口句柄值(每次点击都会增加句柄数)。有人可以帮助我解决我的代码错误。

的index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Selenium Tutorial</title>
</head>

<body>
    <h1>
        <a href="https://mail.google.com" target="_blank">gmail</a>
    </h1>
<h1>
    <a href="https://www.google.co.in" target="_blank">google</a>
</h1>
<h1>
    <a href="https://www.facebook.com/" target="_blank">facebook</a>
</h1>
<h1>
    <a href="http://self-learning-java-tutorial.blogspot.in/"
        target="_blank">Java Tutorial</a>
</h1>

App.java

import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class App {
    public static void waitForNumberOfWindowsToEqual(final WebDriver driver, final int numberOfWindows) {

        while (driver.getWindowHandles().size() != numberOfWindows) {

        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://localhost:8080/application/index.jsp");

        WebElement gmail = driver.findElement(By.linkText("gmail"));
        WebElement javaTutorial = driver.findElement(By.partialLinkText("Tutorial"));

        String mainWindow = driver.getWindowHandle();
        System.out.println("mainWindow handle " + mainWindow);
        System.out.println("Total handles " + driver.getWindowHandles().size());

        gmail.click();
        waitForNumberOfWindowsToEqual(driver, 2);
        String gmailWindow = driver.getWindowHandle();
        System.out.println("gmailWindow handle " + gmailWindow);
        System.out.println("Total handles " + driver.getWindowHandles().size());

        javaTutorial.click();
        waitForNumberOfWindowsToEqual(driver, 3);
        String javaTutorialWindow = driver.getWindowHandle();
        System.out.println("javaTutorialWindow handle " + javaTutorialWindow);
        System.out.println("Total handles " + driver.getWindowHandles().size());

        driver.quit();
    }
}

输出

mainWindow handle {c0582dd6-8cab-b14c-8820-8d8ef12f8658}
Total handles 1
gmailWindow handle {c0582dd6-8cab-b14c-8820-8d8ef12f8658}
Total handles 2
javaTutorialWindow handle {c0582dd6-8cab-b14c-8820-8d8ef12f8658}
Total handles 3

2 个答案:

答案 0 :(得分:1)

driver.getWindowHandle();将返回当前焦点窗口的窗口句柄。要获取所有窗口句柄,请使用复数形式的driver.getWindowHandles()

for (String handle : driver.getWindowHandles()) {
    System.out.println(handle);
}

如果您想要最后打开的窗口,您可以每次添加当前窗口列表并按索引

获取
List<String> handles = new List<String>();

// open new window

for (String handle : driver.getWindowHandles()) {
    if (!handles.contains(handle)) { // add the newly opened window
        handles.add(handle);
    }
}

// open the next window and repeat the loop (you can make a method)

handles将按照创建的顺序显示窗口句柄。并获得最后一个windoe

System.out.println("Last handle " + handles.get(handles.size() - 1));

答案 1 :(得分:1)

如果打开一个新窗口,窗口的上下文不会改变。即使打开了一个新窗口,方法driver.getWindowHandle()也会返回相同的句柄。要与新打开的窗口进行交互,您必须使用driver.switchTo().window(handle);将上下文设置为目标窗口。

此示例在新窗口上设置上下文,以便驱动程序可以与它进行交互:

WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 20);

// open the url
driver.get("http://stackoverflow.com/");

// open a link in a new window with Ctrl + Click
new Actions(driver)
    .keyDown(Keys.CONTROL)
    .click(driver.findElement(By.id("nav-questions")))
    .perform();

// wait for 2 windows
wait.until(ExpectedConditions.numberOfWindowsToBe(2));

// set the context on the new window
Set<String> handles = driver.getWindowHandles();
handles.remove(driver.getWindowHandle());
driver.switchTo().window(handles.iterator().next());

// display the title of the new window
System.out.println("Page title: " + driver.getTitle());

// quit
driver.quit();

请注意,尝试通过获取集的最后一个句柄来获取最后一个窗口

  

Get Window Handles命令返回每个打开的顶级浏览上下文的窗口句柄列表。返回窗口句柄的顺序是任意的。

https://www.w3.org/TR/webdriver/#get-window-handles