最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

getWindowHandle() Selenium Webdriver Javascript - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Apr 28, 2015 at 11:35 Praxis Ashelin 5,2172 gold badges21 silver badges46 bronze badges asked Apr 24, 2015 at 18:33 bfriedrichbfriedrich 531 gold badge1 silver badge5 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 14
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

// 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. 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.

@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

发布评论

评论列表(0)

  1. 暂无评论