How to switch tabs on Chrome in Selenium C#?

前端 未结 1 688
说谎
说谎 2021-01-16 08:28

I am using:

  • Chrome 55.0.2
  • Selenium WebDriver 3.0.1 with ChromeDriver
  • C# 4.6.1 (VS2015 Community Edition)

I am writing automat

相关标签:
1条回答
  • 2021-01-16 09:30

    As a last ditch effort, I am currently searching for a javascript command that I can use with IJavaScriptExecutor to switch tabs - no luck so far.

    I'm providing you a trick using JavaScript to switching between tabs. This is not a perfect solution but you can use as alternate solution. You should try as :-

    //Firstly try to switch window as you're doing 
    Session.Driver.SwitchTo().Window(Session.Driver.WindowHandles.First());
    
    //Now execute a script which would popup the alert box on current window
    IJavaScriptExecutor jscript = Session.Driver as IJavaScriptExecutor;
    jscript.ExecuteScript("alert('Switch tab')");
    
    //Now you can see alert would focused on desired tab
    //Now you can accept this alert and do further steps on this tab
    IAlert alert = Session.Driver.SwitchTo().Alert();
    alert.Accept();
    

    Note:- If you're getting alert exception when trying to accept alert and unable to find alert, You should wait until JavaScript trigger and alert box open.

    0 讨论(0)
提交回复
热议问题