如何通过Selenium获取Chrome上当前处于活动状态的标签索引? [英] How to get currently active tab index on Chrome via Selenium?

查看:511
本文介绍了如何通过Selenium获取Chrome上当前处于活动状态的标签索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建半自动化的应用程序(用户正在打开标签页(注意),如果他想转储其中的一个标签,则只需单击热键即可.)

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];

... 对我不起作用.

... is not working for me.

推荐答案

每次调用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.

您可以在根据您的注释用户切换选项卡(在窗口中),但是驱动程序仍集中在另一个选项卡上,您需要为numberOfWindowsToBe(n)引入 WebDriverWait ,然后您可以找到一个 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上当前处于活动状态的标签索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆