使用Selenium / Protractor Javascript切换到新窗口 [英] Switching to new window with Selenium/Protractor Javascript

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

问题描述

寻找一些帮助,帮助我了解如何在点击登录按钮后触发显示的新弹出窗口。

Looking for some help on how I should be getting ahold of a new "pop-up" window that is triggered to display after I click a "login" button.

我能够在窗口显示时到达,但我不相信我目前用来抓住窗口句柄的代码工作正常。我的情况有点不同,因为我在我的页面中使用量角器,但新窗口出现的不是基于角度的,所以我必须切换到使用selenium WebDriver,而我在那个窗口。 (任何人都知道这种方法是否存在问题?)

I am able to get to when the window is displaying but I do not believe that the code I am currently using to grab the window "handle" is working properly. My situation is a bit different in that I am using protractor inside my pages, but the new window comes up is NOT angular based, so I must switch over to using just selenium WebDriver while I am in that window. (Anyone have any idea if there could be issues with this approach?)

下面你可以找到我用来创建selenium驱动程序的代码片段,以及下面试图切换到/抓住手柄弹出的新窗口。我知道它无法正常工作,因为我一直在尝试在页面上查找表单的代码中收到No Such Element错误。

Below you can find the code snippet that I am using to create the selenium driver, as well as below that trying to "switch to / grab handle" of the new window that is popping up. I know that it is not working correctly because I keep receiving "No Such Element" errors in the code that follows trying to find a form on the page.

    // Create selenium webdriver / driver
    var webdriver = require('selenium-webdriver');

    var driver = new webdriver.Builder().
        withCapabilities(webdriver.Capabilities.chrome()).
        build();

  // Now make sure that the new window is popping up and we are navigating   correctly to it
      var handlePromise = browser.driver.getAllWindowHandles();
      handlePromise.then(function (handles) {
        // parentHandle = handles[0];
        var popUpHandle = handles[1];

        // Change to new handle
        browser.driver.switchTo().window(popUpHandle);

        var popUpHandleFinal = browser.driver.getWindowHandle();
        expect(popUpHandleFinal).toEqual(popUpHandle);
    });

关于此事情:


  1. 如果我删除browser.driver.switchTo()。window(popUpHandle)行中的浏览器,那么它将显示为driver.switchTo()。window(popUpHandle)I收到返回和错误,读作UnknownError:未知错误:'name'必须是非空字符串在对此进行一些搜索后,这是因为驱动程序上的switchTo()方法不能为null。如果我只使用上面显示的代码,则会清除此错误。

  1. If I remove the "browser" in the line "browser.driver.switchTo().window(popUpHandle)" so it reads as "driver.switchTo().window(popUpHandle)" I receive back and error that reads as" UnknownError: unknown error: 'name' must be a nonempty string" After doing some searching on this it is because the "switchTo()" method on driver cannot be null. This error is cleared up if I just use the code shown above.

我不是100%确定我是否应该使用量角器(全局浏览器var )或使用我在此之前设置的直接驱动程序(Selenium)作为获取窗口的方法。

I am not 100% sure if I should be using protractor (global "browser" var) or using the straight "driver" (Selenium) that I set before this as the way to get the windows.

感谢您的帮助

推荐答案

从Protractor(v2.2)的最新版本开始,使用量角器窗口句柄应该没有问题,它会返回当前正在显示的窗口数组。正如P.T指出的那样,不需要调用单独的驱动程序实例,但是浏览器全局变量将起作用。调用弹出窗口的窗口的数组索引为 0 ,弹出窗口的数组索引为 1 。下面是切换到弹出窗口的示例。

As of latest version of Protractor (v2.2) there should not be an issue in using protractor window handles, which returns an array of windows that are currently being displayed. As P.T has pointed out there is no need to invoke a separate driver instance but a browser global variable will work. The window that invokes popup has array index of 0 and popup window will have an array index of 1. Below is a sample of switching to the pop up window to work on it.

browser.getAllWindowHandles().then(function(handles){
    browser.switchTo().window(handles[1]).then(function(){
        //do your stuff on the pop up window
    });
});

希望这有帮助。

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

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