使用 Selenium/Protractor Javascript 切换到新窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28788106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switching to new window with Selenium/Protractor Javascript
提问by parchambeau
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.
寻求有关如何获得在单击“登录”按钮后触发显示的新“弹出”窗口的帮助。
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 WebDriver。(有人知道这种方法是否有问题吗?)
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.
您可以在下面找到我用来创建 selenium 驱动程序的代码片段,以及下面尝试“切换到/抓取”弹出的新窗口的代码片段。我知道它无法正常工作,因为我在尝试在页面上查找表单的代码中不断收到“无此类元素”错误。
// 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);
});
Couple things about this:
关于这一点的几件事:
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.
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.
如果我删除“browser.driver.switchTo().window(popUpHandle)”行中的“browser”,那么它读取为“driver.switchTo().window(popUpHandle)”,我会收到返回错误,读取为“UnknownError” :未知错误:'name' 必须是一个非空字符串” 对此进行一些搜索后,这是因为驱动程序上的“switchTo()”方法不能为空。如果我只使用上面显示的代码,这个错误就会被清除。
我不是 100% 确定我是否应该使用量角器(全局“浏览器”var)或使用我之前设置的直接“驱动程序”(Selenium)作为获取窗口的方式。
Thank you for your help
感谢您的帮助
回答by Girish Sortur
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 browserglobal variable will work. The window that invokes popup has array index of 0and popup window will have an array index of 1. Below is a sample of switching to the pop up window to work on it.
从最新版本的量角器 (v2.2) 开始,使用量角器窗口句柄应该没有问题,它返回当前正在显示的窗口数组。正如 PT 所指出的,不需要调用单独的驱动程序实例,但browser全局变量将起作用。调用 popup 的窗口的数组索引为 ,0而弹出窗口的数组索引为1。下面是切换到弹出窗口进行处理的示例。
browser.getAllWindowHandles().then(function(handles){
browser.switchTo().window(handles[1]).then(function(){
//do your stuff on the pop up window
});
});
Hope this helps.
希望这可以帮助。
回答by Ajay Patil
If you are clicking any link or button in order to open a pop up window, add some wait after click using browser.sleep(ms).
如果您单击任何链接或按钮以打开弹出窗口,请在单击后添加一些等待使用browser.sleep(ms)。
This worked for me and got rid of an error "missing 'handle' parameter"
这对我有用并摆脱了错误“缺少'句柄'参数”
element(by.className("name")).click();
browser.sleep(10000); //This line is important
var winHandles=browser.getAllWindowHandles();
winHandles.then(function(handles)
{
var parentWindow=handles[0];
var popUpWindow=handles[1];
browser.switchTo().window(popUpWindow);
browser.switchTo().window(parentWindow);
})
回答by Manas
If you are calling https://url and again redirect to http://url then this popup is coming because of security issue.
如果您正在调用https://url 并再次重定向到http://url,那么由于安全问题,此弹出窗口即将到来。
1 - You have to configure both application in https://or http://
1 - 您必须在https://或http:// 中配置这两个应用程序

