問題描述
我正在創建半自動化的應用程序(用戶正在打開標簽(注意),如果他想轉儲其中一個,他只需點擊熱鍵).
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 上獲取當前活動的標簽索引?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!