Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从selenium web驱动程序打开chrome中的新选项卡';行不通_C#_Google Chrome_Selenium - Fatal编程技术网

C# 从selenium web驱动程序打开chrome中的新选项卡';行不通

C# 从selenium web驱动程序打开chrome中的新选项卡';行不通,c#,google-chrome,selenium,C#,Google Chrome,Selenium,代码语言c# Selenium Web驱动程序 Actions action = new Actions(BrowserFactory.Driver); action.SendKeys(Keys.Control + "T").Build().Perform(); string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle; 我试图在chrome中打开一个新选项卡,其代码如下

代码语言c# Selenium Web驱动程序

        Actions action = new Actions(BrowserFactory.Driver);
        action.SendKeys(Keys.Control + "T").Build().Perform();
        string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
我试图在chrome中打开一个新选项卡,其代码如下:

        Actions action = new Actions(BrowserFactory.Driver);
        action.SendKeys(Keys.Control + "T").Build().Perform();
        string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
我在stackoverflow上找到了这个代码

        Actions action = new Actions(BrowserFactory.Driver);
        action.SendKeys(Keys.Control + "T").Build().Perform();
        string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
我还尝试:

        Actions action = new Actions(BrowserFactory.Driver);
        action.SendKeys(Keys.Control + "T").Build().Perform();
        string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
        IWebElement body = 
        BrowserFactory.Driver.FindElement(By.TagName("body"));
        body.SendKeys(Keys.Control+'t');
        body.SendKeys(Keys.Control+"t");
这也不起作用

        Actions action = new Actions(BrowserFactory.Driver);
        action.SendKeys(Keys.Control + "T").Build().Perform();
        string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
使用此代码后不会发生任何事情

        Actions action = new Actions(BrowserFactory.Driver);
        action.SendKeys(Keys.Control + "T").Build().Perform();
        string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
有人能帮我一下我做错了什么吗

        Actions action = new Actions(BrowserFactory.Driver);
        action.SendKeys(Keys.Control + "T").Build().Perform();
        string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;

提前感谢。

更好的解决方案不是按CTRL+T或其他键,因为在同一浏览器上的不同浏览器或不同版本上,CTRL+T可能会导致不同的行为

        Actions action = new Actions(BrowserFactory.Driver);
        action.SendKeys(Keys.Control + "T").Build().Perform();
        string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
我更喜欢在浏览器上执行javascript以打开新选项卡的解决方案,因为selenium本机支持在浏览器上注入和执行javascript

        Actions action = new Actions(BrowserFactory.Driver);
        action.SendKeys(Keys.Control + "T").Build().Perform();
        string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
我们应该让javascript在浏览器上执行以下操作:

        Actions action = new Actions(BrowserFactory.Driver);
        action.SendKeys(Keys.Control + "T").Build().Perform();
        string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
  • 创建链接节点,并将链接href设置为“about:blank”或要打开的url,将链接目标设置为“\u blank”

  •         Actions action = new Actions(BrowserFactory.Driver);
            action.SendKeys(Keys.Control + "T").Build().Perform();
            string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
    
  • 将链接节点追加到当前打开页的正文中

  •         Actions action = new Actions(BrowserFactory.Driver);
            action.SendKeys(Keys.Control + "T").Build().Perform();
            string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
    
  • 单击链接并从主体中删除链接

  •         Actions action = new Actions(BrowserFactory.Driver);
            action.SendKeys(Keys.Control + "T").Build().Perform();
            string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
    
    代码示例:

            Actions action = new Actions(BrowserFactory.Driver);
            action.SendKeys(Keys.Control + "T").Build().Perform();
            string secondTabHandle = BrowserFactory.Driver.CurrentWindowHandle;
    
    string newTabScript = "var d=document,a=d.createElement('a');"
    + "a.target='_blank';a.href='{0}';"
    + "a.innerHTML='new tab';"
    + "d.body.appendChild(a);"
    + "a.click();"
    + "a.parentNode.removeChild(a);"
    
    public void newTab(string tabUrl) 
    {
      if(String.IsNullOrEmpty(tabUrl) {
        tabUrl = "about:blank";
      } 
      IWebDriver driver; // assume assigned elsewhere
      IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
      js.ExecuteScript(String.format(newTabScript, tabUrl));
    }
    

    忽略它是Java这一事实的可能重复,就像与c相关一样,我也尝试了IWebElement body=BrowserFactory.Driver.findelelement(By.TagName(“body”);body.SendKeys(key.Control+'t');正如在那页上解释的,但那没有任何作用。你也有在选项卡之间切换的代码吗?对于chrome浏览器,你可以像上面说的那样尝试,切换后有一个提醒,你可能注意到活动选项卡没有更改,但实际上,在切换的选项卡上操作的脚本应该可以工作。@Yong对于新浏览器(不是新选项卡),你会怎么做