简体   繁体   English

获取当前活动标签

[英]Get current active tab

I'm using Selenium and C#, the Web driver. 我正在使用Selenium和Web驱动程序C#。 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() 我一直在尝试所有C#API方法来执行此任务,例如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. 上面的代码的问题是它在多个选项卡之间闪烁,因为我在选项卡之间切换,我认为这是正确的。

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);
//...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM