[英]Selenium chrome webDriver - get focus on the latest open new tab java
I have problem while trying to get focus on the latest new tab i'v opened with selenium web driver.我在尝试专注于我使用 selenium Web 驱动程序打开的最新标签时遇到问题。
in some mysterious way, when i try to change between 2 tabs it works, but when i'm trying to do this with 4 tabs it always get focus on the 3rd tab.以某种神秘的方式,当我尝试在 2 个选项卡之间进行更改时,它可以工作,但是当我尝试使用 4 个选项卡执行此操作时,它总是将注意力集中在第 3 个选项卡上。
here is the code of the function that gets WebDriver element:这是获取 WebDriver 元素的函数的代码:
ArrayList tabs = new ArrayList(driver.getWindowHandles());
System.out.println(tabs);
System.out.println(tabs.get(tabs.size() -1));
driver.switchTo().window((String) tabs.get(tabs.size() - 1));
and here is the out put when i'm printing the tabs:这是我打印标签时的输出:
[CDwindow-(43C81B1D7B7C666BFBFB339971ADEE0F), CDwindow-(CDD5D45D021E698DD005BC2AD6201714), CDwindow-(33FB208B51A8DD5F7DB947C7B0BAB9DD), CDwindow-(4B50A8C4074BEDB41152003DED37FB32)]
CDwindow-(4B50A8C4074BEDB41152003DED37FB32)
as you can see the first row is the all the ArrayList.如您所见,第一行是所有的 ArrayList。 and the second row is only the last tab that i want to get focus on.
第二行只是我想要关注的最后一个选项卡。
am i missing something here??我在这里错过了什么吗?
I fail to see the issue.我看不到问题。
tabs
holds a list of handles (which may or may not be in the order you see them in the browser). tabs
包含一个句柄列表(可能与您在浏览器中看到的顺序不同)。 tabs.size()
is returning 4 (4 tabs). tabs.size()
返回 4(4 个标签)。 so tabs.get(tabs.size() - 1))
is tabs.get(4 -1 )
or tabs.get(3)
.所以
tabs.get(tabs.size() - 1))
是tabs.get(4 -1 )
或tabs.get(3)
。
Since java is 0-indexed, tabs.get(3)
is the last tab.由于 java 是 0 索引的,因此
tabs.get(3)
是最后一个选项卡。 And looking at the output above is correct;查看上面的输出是正确的; CDwindow-(4B50A8C4074BEDB41152003DED37FB32) .
CDwindow-(4B50A8C4074BEDB41152003DED37FB32) 。
Where is it going to the third tab;它会去哪里到第三个选项卡; CDwindow-(33FB208B51A8DD5F7DB947C7B0BAB9DD)?
CDwindow-(33FB208B51A8DD5F7DB947C7B0BAB9DD)?
I find it easier to keep track of if you Map the windows to a name:如果将窗口映射到名称,我发现更容易跟踪:
public static Map<String, String> tabs = new HashMap<String, String>();
With the first String being your own descriptor like "Admin", or "Facebook" and the second String is the WindowHandle.第一个字符串是您自己的描述符,如“Admin”或“Facebook”,第二个字符串是 WindowHandle。 You will need to add the windows one by one to keep track of them.
您需要一一添加窗口以跟踪它们。
tabs.put(handleName, handle);
But when you want to switch you can use:但是当你想切换时,你可以使用:
driver.switchTo().window(tabs.get("Admin"));
And eliminate all confusion.并消除所有的混乱。
So, after a little bit of debugging - i just wrote an if statement the will stop on the page i want.因此,经过一些调试后 - 我刚刚写了一个 if 语句,它将停在我想要的页面上。
Set<String> handles = driver.getWindowHandles();
String currentHandle = driver.getWindowHandle();
for (String handle : handles) {
if (!handle.equals(currentHandle)) {
driver.switchTo().window(handle);
if(whatImLookingFor)
break;
}
}
the problem is solved for me, but i think it worth to try to understand this for future needs...问题对我来说已经解决了,但我认为值得尝试理解这一点以满足未来的需求......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.