不适用newtab的Selenium Webdriver C#Sendkeys(Keys.control +“ t”)

时间:2018-11-05 08:11:33

标签: c# selenium selenium-webdriver

我想在硒c#中创建this.windowfirefox新标签

尝试3种解决方案,但不适用于我。

firefox和webdrive是最新版本。

.netframework 4.5

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.Threading;

namespace Testselenium
{
    class Program
    {
        static void Main(string[] args)
        {

            var drive3 = new FirefoxDriver();
            drive3.Navigate().GoToUrl("http://www.google.com");

            IWebElement element21 = drive3.FindElement(By.TagName("body"));
            System.Threading.Thread.Sleep(5000);
           // element21.Click();
             element21.SendKeys(Keys.Control + "t");
            // element21.SendKeys(Keys.LeftControl + "t");
             //element21.SendKeys(Keys.Control + "T");



        IWebDriver driver = new FirefoxDriver();

            driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
            driver.SwitchTo().Window(driver.WindowHandles.Last());
            driver.Navigate().GoToUrl("http://www.google.com");

        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以尝试使用JavaScriptExecutor打开新标签页:

IJavaScriptExecutor js = (IJavaScriptExecutor)drive3;
js.ExecuteScript("window.open('http://www.google.com');");

这应该可以让您在新标签页中打开Goog​​le主页。如果您想打开空白页-只需不要将参数传递给window.open()