简体   繁体   中英

How can i open a new tab using protractor and Chrome browser

Here is a code (new tab doesn't open):

//open new tab in Chrome

browser.actions().sendKeys(protractor.Key.CONTROL +'t').perform();

If we used code with 'a' - everything is fine:

//select all on the page

browser.actions().sendKeys(protractor.Key.CONTROL +'a').perform();

protractor v.1.3.1

Chrome v.37

ChromeDriver v.2.10

WebDriver v.2.43

If you really don't want to add an element to your DOM, then you can try this:

let url = https://google.com;
return browser.executeScript("return window.open(arguments[0], '_blank')", url);
//opens google.com in a new tab (works fine with Chrome. P.S. have only tested
// Chrome with Protractor).

I had tried the above statement with a browser.wait() , see if you really need the wait as browser.executeScript() returns a promise itself, can just utilize the promise's success.

Also, I have observed that although it seems that the focus of the browser has changed to the newly opened tab, I was unable to access the elements of the new tab. To do that:

browser.getAllWindowHandles().then((handles) => {
    browser.switchTo().window(handles[1]);    // pass the index, here assuming that
                                              // there are only two tabs in the browser
})

To know more about window.open() , you can visit this.

Selenium doesn't provide a way to do this so a workaround seems to be the only way. Assuming you're in Windows or Linux, your CTRL+T idea should be written as below, however that hack failed for me:

browser.actions().keyDown(protractor.Key.CONTROL).sendKeys('t').perform();

Even attempting to do it on an element:

$$('input').first().sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "t"));

Good news is the following hack does seem to work, feel free to replace location.href with the url you want to open:

browser.driver.executeScript(function() {
  (function(a){
  document.body.appendChild(a);
  a.setAttribute('href', location.href);
  a.dispatchEvent((function(e){
    e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null);
    return e;
  }(document.createEvent('MouseEvents'))))}(document.createElement('a')));
});

我认为在ChromeDriver中“打开一个新标签”的问题,我发现了一个类似这样的错误: https//code.google.com/p/chromedriver/issues/detail? id = 903&q = new%20tab&cfpec = ID%20Status% 20Pri%20Owner%20Summary

This piece of code works for me in TypeScript with protractor.

import {browser} from 'protractor';

export class Browser {
  public async openPageInNewTab(url: string) {
    await this.createNewBrowserTab();
    await this.switchToTabNumber(1);
    await browser.get(url);
  }

  public createNewBrowserTab() {
    browser.executeScript('window.open()');
  }

  public async switchToTabNumber(number: number) {
    return browser.getAllWindowHandles().then(function (handles) {
      const newWindowHandle = handles[number];
      browser.switchTo().window(newWindowHandle);
    });
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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