getWindowHandle() Selenium Webdriver Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29855006/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:11:43  来源:igfitidea点击:

getWindowHandle() Selenium Webdriver Javascript

javascriptseleniumcoffeescript

提问by bfriedrich

Made some changes based on help from engineering. Here is the final code I used for grabbing the new window handle:

根据工程的帮助做了一些更改。这是我用于获取新窗口句柄的最终代码:

localdriver = @driver
@driver.getAllWindowHandles()
.then (handles) ->
    localdriver.switchTo().window(handles[1])

I'm currently running an automation stack that uses Selenium Webdriver, Mocha, Chai, and Grunt. I'm creating scripts in Coffeescript, but an answer to my question in Javascript would be perfectly fine.

我目前正在运行一个使用 Selenium Webdriver、Mocha、Chai 和 Grunt 的自动化堆栈。我正在用 Coffeescript 创建脚本,但是用 Javascript 来回答我的问题会非常好。

What I'm trying to do:

我正在尝试做的事情:

  • Click button on main browser window
  • Switch driver to the second window that opens after button click
  • Perform actions in the second window
  • Close second window and return to the first.
  • 单击主浏览器窗口上的按钮
  • 将驱动程序切换到单击按钮后打开的第二个窗口
  • 在第二个窗口中执行操作
  • 关闭第二个窗口并返回到第一个窗口。

I've scoured the internet looking for an answer on how to do this. Just started learning all this stuff a few months ago, and I'm still stumbling through creating stuff. I'm seeing a lot of Java and C+ examples, but not much on the Javascript side. Can anyone provide an example of how to set up the code for the above scenario using Selenium Webdriver and Javascript?

我已经在互联网上搜寻有关如何执行此操作的答案。几个月前才开始学习所有这些东西,我仍然在创造东西的过程中跌跌撞撞。我看到了很多 Java 和 C+ 示例,但在 Javascript 方面并不多。谁能提供一个示例,说明如何使用 Selenium Webdriver 和 Javascript 为上述场景设置代码?

回答by Rohn Adams

var parent = driver.getWindowHandle();
var windows = driver.getAllWindowHandles();

driver.switchTo().window(windows[1]);

// do some stuff

driver.close();
driver.switchTo().window(parent);

回答by Jeremy Moritz

What you want is driver.getAllWindowHandles(), but because this returns a promise, make sure that you then use the handles inside of the thenfunction

你想要的是 driver.getAllWindowHandles(),但因为这会返回一个承诺,所以请确保然后使用then函数内部的句柄

// select the newly opened window
driver.getAllWindowHandles().then(function gotWindowHandles(allhandles) {
    driver.switchTo().window(allhandles[allhandles.length - 1]);
});

回答by Jai Prak

Whenever new tab opens, it takes some time to come up and render. In this situation, it is difficult to switch the tab because the tab is not opened yet and driver.getAllWindowHandles()will not give handler for that tab. I solved this problem in this way, I am assuming I have one opened tab and on some button click, I am opening new 2nd tab.

每当新选项卡打开时,都需要一些时间来呈现。在这种情况下,很难切换选项卡,因为选项卡尚未打开并且driver.getAllWindowHandles()不会为该选项卡提供处理程序。我以这种方式解决了这个问题,我假设我打开了一个选项卡,单击某个按钮后,我将打开新的第二个选项卡。

function openNewTab(driver) {
  driver.wait(function () {
    return driver.getAllWindowHandles().then(function (handles) {
      var isHandleCount2 = (handles.length == 2);
      if (isHandleCount2) {
        driver.switchTo().window(handles[1]);
      }
      return isHandleCount2;
    });
  }).then(function () {
  // Now do some stuff in new tab
    var buttonElement = driver.wait(until.elementLocated(By.xpath("//td[*//span[text()='Click me']]")));
    buttonElement.click();
  });
} 

This code will wait until the number of handles or tabs will not equal to 2.

此代码将等到句柄或选项卡的数量不等于 2。

回答by ColinWa

@Jai Prak's answer is brilliant.
What about the case of three tabs or more?
The newest tab will always be the last Tab.

@Jai Prak 的回答很棒。
三个标签或更多标签的情况如何?
最新的标签将始终是最后一个标签。

return await driver.wait(async function () {
    return await driver.getAllWindowHandles().then(async function (handles) {
       // var isHandleCount2 = (handles.length == 2);
        if (handles.length > 1) {
            return driver.switchTo().window(handles[handles.length - 1]);
        }
        return false;
    });
}).then(function () {
    // Now do some stuff in new tab

});


The above will apply except in cases you switch between Tabs.
To move the next tab, get the current Tab's index -1


以上将适用,除非您在选项卡之间切换。
要移动下一个选项卡,请获取当前选项卡的index -1