3

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?

4 Answers 4

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

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

// do some stuff

driver.close();
driver.switchTo().window(parent);
Sign up to request clarification or add additional context in comments.

7 Comments

driver.getWindowHandles() is not a function
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/…
You're looking at the java documentation. In JavaScript, the correct method is driver.getAllWindowHandles()
Thank you @Jeremy-DeerAngel-org, been chasing my tail on this for hours
@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?
|
5

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]);
});

3 Comments

Hi, I tried your code, but it said "getAllWindowHandles is not a function in <eval> ..."
@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. :-/
@Ragnarsson await driver.getAllWindowHandles().then(async (allhandles) => { return await driver.switchTo().window(allhandles[allhandles.length - 1]); });
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. 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.

1 Comment

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 () { });
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.