0

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();
        }
    }
}
PEYMAN
  • 55
  • 1
  • 1
  • 5

2 Answers2

1

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?

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
Ankur Singh
  • 1,239
  • 1
  • 8
  • 18
0

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")
}
mahan
  • 12,366
  • 5
  • 48
  • 83