如何使用Selenium切换到新窗口? [英] How to switch to a new window using Selenium?

查看:2607
本文介绍了如何使用Selenium切换到新窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我已经使用selenium到达了一个页面,有一个链接在同一页面上作为弹出窗口打开,但该链接正在使用,所以它就像一个新页面在新窗口中打开。

In my code i have reached to a page using selenium in whihc there is a link which opens as a popup on the same page, but that link is using , so it is like a new page which is opening in new window.

我可以点击该链接并打开页面,但是当我正在执行driver.getWindowHandles()时,它返回的大小仅为1而不是2,因为我无法切换到新窗口。

I am able to click to that link and open the page to, but when i am doing driver.getWindowHandles(), it is returning the size as 1 only and not 2, because of which i am not able to switch to new window.

以下是我正在使用的代码:

Below is the code that i am using:

    String parent = driver.getWindowHandle();
    driver.findElement(By.xpath("//a[@id='abc']")).click();
    // after clicking on the link
    try{
    Thread.sleep(1000);

    Set<String> availableWindows = driver.getWindowHandles();//this set size is
   // returned as 1 and not 2
    String newWindow = null;
    for (String window : availableWindows) {
        if (!parent.equals(window)) {
            newWindow = window;
        }
    }
    assertNotNull(newWindow);

    // switch to new window
    driver.switchTo().window(newWindow);
    // do assert the elements in the new window
    // and then close the new window
    driver.close();
    // switch to parent
    driver.switchTo().window(parent);
    // close main window
    driver.close();}
    catch(Exception e){

由于弹出窗口是主窗口本身的一部分,即为什么我无法通过执行getWindowHandle()来获得正确的大小;

Since the popup window is the part of the master window itself, ie, why i am not able to get the correct size by doing getWindowHandle();

但我的要求是仅保存弹出页面。
现在,每次调用母版页驱动程序时,保存代码都会保存母版页详细信息以及弹出内容。

but my requirement is to save the popup page only. Right now the save code is saving the master page details along with the popup content since every time the masterpage driver is called.

是否有任何解决方法,我可以做到只获得弹出页面的驱动程序?

Is there any workaround that i can do to get the driver of the popup page only?

保存代码是通用的,在此引用中并不重要。
我想要的只是获取弹出页面的驱动程序

Save code is generic and that is not important in this reference. All i want is to get the driver of the popup page only

推荐答案

点击链接后需要等待直到Selenium意识到现在共有2个窗口。

After clicking the link you need to wait until Selenium realizes there are now totally 2 windows.

String parent = driver.getWindowHandle();
    driver.findElement(By.xpath("//a[@id='alertHistoryLink']")).click();
    // after clicking on the link
    // call the method numberOfWindowsToBe as follows
    WebDriverWait wait = new WebDriverWait(driver, 15, 100);
    wait.until(numberOfWindowsToBe(2));

public static ExpectedCondition<Boolean> numberOfWindowsToBe(final int numberOfWindows) {
    return new ExpectedCondition<Boolean>() {
      @Override
      public Boolean apply(WebDriver driver) {
                driver.getWindowHandles();
        return driver.getWindowHandles().size() == numberOfWindows;
      }
    };
}




我想要的是得到的驱动程序仅限弹出页面

All i want is to get the driver of the popup page only

编辑:主窗口的驱动程序是弹出窗口的弹出窗口驱动程序本身就是主窗口的一部分。唯一的例外是,如果弹出窗口存在于框架中,则需要切换到框架,然后您可以对弹出窗口中的元素执行操作。

The driver for the master window is the driver for the popup as popup itself is the part of the master window. The only exception is, if the popup is present in a frame, then you need to switch to the frame then you can perform actions on the elements present in the popup.

编辑:如果我理解正确,你想获得弹出窗口的页面源代码。由于弹出窗口是主窗口的一部分。我要做的是

If I understand you correctly, you want to get the page source of the popup. As popup is part of the master window. What I would do is

popup.getAttribute(innerHTML);
弹出窗口类型为WebElement。

popup.getAttribute("innerHTML"); Where popup is of type WebElement.

例如,如果主窗口的HTML类似于

For example if HTML of the master window is like

 <html>
<head>
.....
</head>
<body>
...
...
..
<div id = popup> 
//html elements present in popup
</div>
...
..
</head>
</html>

然后

WebElement popup = driver.findElement(By.xpath(// div [@ id ='popup']));

String htmlInsidePopup = popup.getAttribute("innerHTML");
System.out.println(htmlInsidePopup);

将解决问题。

这篇关于如何使用Selenium切换到新窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆