Friday 2 September 2016

Opening new tab using selenium webdriver with C# [Chrome]

Ooops! After a lot of googling came up with this fare solution. Made my day!! 


Previously, I was trying this code:
//IWebElement body = webDriver.FindElement(By.TagName("body"));
//body.SendKeys(Keys.Control + 't');

But, it was neutral for me. 


IWebDriver webDriver = new ChromeDriver();

webDriver.Manage().Window.Maximize();
webDriver.Navigate().GoToUrl(“www.google.co.in”);
              
//IWebElement body = webDriver.FindElement(By.TagName("body"));
//body.SendKeys(Keys.Control + 't');

IJavaScriptExecutor js = webDriver as IJavaScriptExecutor;
js.ExecuteScript("window.open();");           

webDriver.SwitchTo().Window(webDriver.WindowHandles[1]);//Making 2nd Tab Active.
webDriver.Navigate().GoToUrl("https://yahoo.com/");

webDriver.Close();//Closing Active Tab (2nd Tab)
           
webDriver.SwitchTo().Window(webDriver.WindowHandles[0]);// Making Tab Active.




 Hope this will help you!

Cheers!





No comments:

Post a Comment