javascript 如何使用量角器和 Chrome 浏览器打开新标签页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26098593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:29:53  来源:igfitidea点击:

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

javascriptselenium-webdriverprotractor

提问by Oleg V

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

这是一个代码(新选项卡未打开):

//open new tab in Chrome

//在Chrome中打开新标签

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

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

如果我们使用带有“a”的代码 - 一切都很好:

//select all on the page

//全选页面

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

protractor v.1.3.1

量角器 v.1.3.1

Chrome v.37

铬 v.37

ChromeDriver v.2.10

ChromeDriver v.2.10

WebDriver v.2.43

WebDriver v.2.43

回答by Ashish Ranjan

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

如果你真的不想在你的 DOM 中添加一个元素,那么你可以试试这个:

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.

我已经用 a 尝试了上面的语句browser.wait(),看看你是否真的需要等待,因为browser.executeScript()返回一个承诺本身,可以利用承诺的成功。

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.

要了解更多信息window.open(),您可以访问此。

回答by Leo Gallucci

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:

Selenium 没有提供执行此操作的方法,因此解决方法似乎是唯一的方法。假设你使用的是 Windows 或 Linux,你的 CTRL+T 想法应该写如下,但是这个 hack 对我来说失败了:

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.hrefwith the url you want to open:

好消息是以下 hack 似乎确实有效,请随意替换location.href为您要打开的 url:

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

回答by Leszek_PL

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

这段代码在带有量角器的 TypeScript 中对我有用。

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

}

回答by Oleg V

I think problem with "open a new tab" in ChromeDriver, I found a bug like this: https://code.google.com/p/chromedriver/issues/detail?id=903&q=new%20tab&colspec=ID%20Status%20Pri%20Owner%20Summary

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