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

查看:23
本文介绍了使用 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 驱动程序的代码片段,以及尝试切换到/抓取"弹出的新窗口的句柄的代码片段.我知道它无法正常工作,因为我在尝试在页面上查找表单的代码中不断收到无此类元素"错误.

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)"行中的browser",所以它读作driver.switchTo().window(popUpHandle)",我会收到回复并读取为UnknownError:未知错误:'name'必须是非空字符串"的错误在对此进行一些搜索后,这是因为驱动程序上的switchTo()"方法不能为空.如果我只是使用上面显示的代码,这个错误就会被清除.

  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 指出的那样,不需要调用单独的驱动程序实例,但 browser 全局变量将起作用.调用 popup 的窗口的数组索引为 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天全站免登陆