getWindowHandle() Selenium Webdriver Javascript

14,540

Solution 1

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

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

// do some stuff

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

Solution 2

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

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

Solution 3

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.

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.

Share:
14,540
bfriedrich
Author by

bfriedrich

Updated on June 14, 2022

Comments

  • bfriedrich
    bfriedrich almost 2 years

    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.

    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?

  • Jeremy Moritz
    Jeremy Moritz about 8 years
    driver.getWindowHandles() is not a function
  • Rohn Adams
    Rohn Adams about 8 years
    Are you sure? It is still referenced on the selenium documentation pages: seleniumhq.org/docs/03_webdriver.jsp <--- scroll down to "Moving between windows and frames". It is also still listed here under this documentation: selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/…
  • Jeremy Moritz
    Jeremy Moritz about 8 years
    You're looking at the java documentation. In JavaScript, the correct method is driver.getAllWindowHandles()
  • Rick Runyon
    Rick Runyon over 7 years
    Thank you @Jeremy-DeerAngel-org, been chasing my tail on this for hours
  • Ragnarsson
    Ragnarsson about 6 years
    Hi, I tried your code, but it said "getAllWindowHandles is not a function in <eval> ..."
  • Jeremy Moritz
    Jeremy Moritz about 6 years
    @Ragnarsson I’m not sure what’s going on in your specific case. It may be no comfort to know this but it worked on my machine. Unfortunately, it's been two years since i worked with that code, so I'm not able to troubleshoot it now. :-/
  • supersan
    supersan over 5 years
    @JeremyMoritz I think the accepted answer must be changed to reflect this. Anyway where did you find out about getAllWindowHandles. I can find any docs which have a list of functions including this for node?
  • Jeremy Moritz
    Jeremy Moritz over 5 years
    @supersan you can find it referenced by an eloquent man in the comments section at this link: stackoverflow.com/questions/29855006/…
  • Brian
    Brian almost 5 years
    Maybe we have a different running environment, but this just doesn't work on modern Node. The driver stays on the original tab, and will close the parent tab, then crash with an exception. parent and windows are promises, so the JS runtime will not wait for them to resolve.
  • ColinWa
    ColinWa over 4 years
    @Ragnarsson await driver.getAllWindowHandles().then(async (allhandles) => { return await driver.switchTo().window(allhandles[allhandles.length - 1]); });
  • ColinWa
    ColinWa over 4 years
    This is brilliant: +1. What about the case of two tabs or more? The newest tab will always be the last Tab. i made slight changes: return await driver.wait(async function () { return await driver.getAllWindowHandles().then(async function (handles) { if (handles.length > 1) { return driver.switchTo().window(handles[handles.length - 1]); } return false; }); }).then(function () { });