简体   繁体   中英

c# selenium firefox open new tab is not working

i try this codes and open mozila firefox the get google.com site but never open new tab why?

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://google.com");
            IWebElement element = driver.FindElement(By.TagName("Body"));
            System.Threading.Thread.Sleep(6000);
            element.SendKeys(OpenQA.Selenium.Keys.Control + "t");
        }
    }
}

and try this but it has never open new tab!

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://google.com");
            var action = new Actions(driver);
            System.Threading.Thread.Sleep(6000);
            action.KeyDown(Keys.Control).SendKeys("t").Perform();
        }
    }
}

You can use javascript executor for this :-

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open('https://www.google.com','_blank');");



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


 IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://google.com");
            IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
          js.ExecuteScript("window.open();");
        }
    }
}

Refer this link :- How to handle the new window in Selenium WebDriver using Java?

Find body and then send keys.

 static void Main(string[] args) { IWebDriver driver = new FirefoxDriver(); driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t"); driver.SwitchTo().Window(driver.WindowHandles.Last()); driver.Navigate().GoToUrl("https://www.google.com") } 

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