问题描述
我正在创建半自动化的应用程序(用户正在打开标签(注意),如果他想转储其中一个,他只需点击热键).
I'm creating app which is half-automated (user is opening tabs (attention) and if he wants to dump one of them he just clicks hot-key).
但是当用户打开很多标签时,我需要知道我应该切换到哪一个.我如何获得 currenttab 索引.或者切换到 Selenium C# 上的当前选项卡?
But when user opens to much tabs, I need to know to which one I should switch. How can i get currenttab index. Or switch to current tab on Selenium C#?
string windowHandle = Browser.WindowHandles.Last();
string windowHandle = Browser.WindowHandles.First();
string windowHandle = Browser.WindowHandles[1];
...不适合我.
推荐答案
每次调用 Browser.WindowHandles()
时,currenttab 索引可能会发生变化.
The currenttab index may get changed everytime you invoke Browser.WindowHandles()
.
虽然一般的看法是 WindowHandles 会像最旧的窗口在前排序,最新的窗口在后.但事实并非如此:它是完全随机的!
在一次讨论中,西蒙明确提到:
In a discussion, Simon clearly mentioned:
虽然用于存储句柄列表的数据类型可以按插入排序,但 WebDriver 实现迭代窗口句柄以插入它们的顺序不需要稳定.顺序是任意的.
While the datatype used for storing the list of handles may be ordered by insertion, the order in which the WebDriver implementation iterates over the window handles to insert them has no requirement to be stable. The ordering is arbitrary.
此评论与 获取窗口句柄 它提到的部分:
This comment is pretty much inline with the Get Window Handles section where it mentioned:
为了判断与浏览器的特定交互是否打开了新窗口,可以获取交互执行前的窗口句柄集合,并与执行操作后的集合进行比较.
In order to determine whether or not a particular interaction with the browser opens a new window, one can obtain the set of window handles before the interaction is performed and compare it with the set after the action is performed.
您可以在 使用 Selenium 的 WindowHandles 跟踪和迭代选项卡和窗口的最佳方式
根据您的评论 用户切换选项卡(在窗口中)但驱动程序仍专注于另一个选项卡您需要为 numberOfWindowsToBe(n)<诱导 WebDriverWait/code>,您可以在 getWindowHandles() 在 Firefox 58 中不起作用.焦点仍然在父标签上,不会转移到下一个标签
As per your comment user switch tab (in window) but driver is still focused on another tab you need to induce WebDriverWait for numberOfWindowsToBe(n)
and you can find a detailed discussion in getWindowHandles() not working in firefox 58.The focus remains on parent tab and does not transfer to next tab
这篇关于如何通过 Selenium 在 Chrome 上获取当前活动的标签索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!