Bipin
Bipin

Reputation: 451

Get current active tab

I'm using Selenium and C#, the Web driver. I want to query the current tab and analyze it for certain things. If useropens new Tab then the new tabs should be queried.

I have been trying all the C# API methods to do this task such as SwitchTo()

foreach (string s in this.driver.WindowHandles){
  this.driver.SwitchTo().Window(s);
  if (((IJavaScriptExecutor)this.driver).ExecuteScript("return document.hidden").Equals("false")) {

    IJavaScriptExecutor scriptExe = (IJavaScriptExecutor)this.driver;
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
    wait.Until<bool>(Function);//function evaluation certain expression...
    scriptExe.ExecuteScript("alert(\"Something\");");
  }
}

Problem with the above code is it flickers between multiple tab as I think is right because I'm switching between the tabs.

Upvotes: 0

Views: 1207

Answers (1)

Guy
Guy

Reputation: 50809

If you want the last opened tab you need to use the last window handle instead of iterating over all of them in a loop

string windowHandle = this.driver.WindowHandles.Last() // using System.Linq
this.driver.SwitchTo().Window(windowHandle);
//...

Upvotes: 1

Related Questions