ad

Sunday, March 27, 2016

Selenium WebDriver in C#: Switch To new window or tab

There are many instances when you click on a link and it opens a new window or tab. Selenim WebDriver provides a way to switch to that new window using SwitchTo() method.
See the code below
 
IWebDriver driver = new new FirefoxDriver();

//perform some action to open a new window. Like clicking a link.
driver.FindElement(By.Id("btnId")).Click();

//switch to new window.
driver.SwitchTo().Window(driver.WindowHandles.Last());

//if you want to switch back to your first window
driver.SwitchTo().Window(driver.WindowHandles.First());
I hope this little code snippet will help you in your future automation :)

10 comments:

  1. It worked for me. I love it man!

    ReplyDelete
  2. what if we want to handle OS window without using autoIT ?

    ReplyDelete
  3. Hello, It is not working for my workflow, Please help me for below workflow

    1.Open URL, let say abc.com
    2. open second window say xyz.com and switch to this window using above code and it will work as exptected
    3. click download button which is exist in page, it will file download window dialog popup, now I used AUTOIT and able to download by click save button of download
    3 . Open new window and able to switch that new window
    4 closed newly created window and try to switch to 2nd window which had window popup earlier for download, I always get exception of timeout, yes I can increase but it still will not work.

    Can you please help

    ReplyDelete
  4. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.selenium training in bangalore

    ReplyDelete
  5. It's so nice of you to share this snippet here! I'd tried to make out some by myself, but I'm sort of not so good with programming scripts, so my one has just crashed my laptop to death :D So, I did fresh reinstall of windows, there were some issues with dynamic libraries right away, but I've fixed them in easy fashion with using this tool to troubleshoot all the errors like msvcr100.dll missing https://fix4dll.com/msvcr100_dll so my laptop is alive and kicking for now. Anyway, let's check this one snippet of yours

    ReplyDelete
  6. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.
    software testing course in chennai

    ReplyDelete
  7. Thanks for the useful information of software testing courses, give more updates on software testing development, First time I visit your blog really nice, I bookmark your blog here after a daily visit. Learn software testing course in Chennai

    ReplyDelete
  8. Hi,Nice blog.Thank you so much for sharing this.If you are looking for an used car please visit us https://www.dreamcaronline.com/

    ReplyDelete
  9. Nice post.Thank you so much for sharing.Yiioverflow is a web development company.We have well expert team in Angular JS, Ionic, Yii Framework, Node JS, Laravel, PHP, MySQL, and WordPress.If you want a developer visit.. https://yiioverflow.com/

    ReplyDelete