【问题标题】:How can i open a new tab using protractor and Chrome browser如何使用量角器和 Chrome 浏览器打开新标签
【发布时间】:2014-11-23 18:27:12
【问题描述】:

这是一个代码(新标签没有打开):

//在Chrome中打开新标签

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

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

//全选页面

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

量角器 v.1.3.1

Chrome v.37

ChromeDriver v.2.10

WebDriver v.2.43

【问题讨论】:

    标签: javascript selenium-webdriver protractor


    【解决方案1】:

    这段代码在 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);
        });
      }
    
    }
    

    【讨论】:

      【解决方案2】:

      如果你真的不想在你的 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).
      

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

      另外,我观察到,虽然浏览器的焦点似乎已更改为新打开的选项卡,但我无法访问新选项卡的元素。为此:

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

      想了解更多window.open(),可以访问this.

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

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

          甚至试图在一个元素上这样做:

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

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

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

          【讨论】:

          • 很好,里奥。我尝试了前两个,但也失败了……不知道第三个。谢谢!
          • 第三个是和我一起工作的。只需添加 a.setAttribute('target', 'blank');
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-11-16
          • 2015-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-19
          相关资源
          最近更新 更多