bfriedrich
bfriedrich

Reputation: 53

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. I'm creating scripts in Coffeescript, but an answer to my question in Javascript would be perfectly fine.

What I'm trying to do:

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?

Upvotes: 3

Views: 17995

Answers (4)

ColinWa
ColinWa

Reputation: 999

@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

Upvotes: 0

Rohn Adams
Rohn Adams

Reputation: 838

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

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

// do some stuff

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

Upvotes: 14

Jai Prak
Jai Prak

Reputation: 3410

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.

Upvotes: 1

Jeremy Moritz
Jeremy Moritz

Reputation: 14902

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

Upvotes: 5

Related Questions