简体   繁体   English

getWindowHandle()Selenium Webdriver Javascript

[英]getWindowHandle() Selenium Webdriver Javascript

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. 我当前正在运行一个使用Selenium Webdriver,Mocha,Chai和Grunt的自动化堆栈。 I'm creating scripts in Coffeescript, but an answer to my question in Javascript would be perfectly fine. 我正在用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. 我看到了很多Java和C +示例,但在Javascript方面却很少。 Can anyone provide an example of how to set up the code for the above scenario using Selenium Webdriver and Javascript? 谁能提供使用Selenium Webdriver和Javascript为上述场景设置代码的示例?

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

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

// do some stuff

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

What you want is driver.getAllWindowHandles(), but because this returns a promise, make sure that you then use the handles inside of the then function 您想要的是driver.getAllWindowHandles(),但是由于这将返回一个Promise,因此请确保您随后使用then函数中的句柄

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

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. 在这种情况下,很难切换选项卡,因为尚未打开选项卡,并且driver.getAllWindowHandles()不会为该选项卡提供处理程序。 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. 我以这种方式解决了这个问题,假设我有一个打开的选项卡,单击某些按钮,就打开了新的第二个选项卡。

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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM