WebDriver.getWindowHandle()方法 [英] WebDriver.getWindowHandle() method

查看:1603
本文介绍了WebDriver.getWindowHandle()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Selenium学习的新手。 WebDriver.getWindowHandle()文档对我来说不是很清楚,并且示例没有按照书中的说明进行操作,因此我想到确认此方法返回的值。

I'm new to Selenium learning. WebDriver.getWindowHandle() documentation is not very clear to me and the example is not working as given in the book, so I thought of confirming the value returned by this method.

1)假设我在第PAGE1页。所以getWindowHandle()应该返回PAGE1的句柄。 (正确)

1) Let's say I am on page PAGE1. So getWindowHandle() should return handle to PAGE1. (Correct)

2)现在从这个页面,我转到PAGE2(通过超链接并打开一个新窗口)。我的书说现在getWindowHandle()应该返回PAGE2的句柄。 然而我的程序仍然返回PAGE1的句柄。

2) Now from this page, I go to PAGE2 (by hyperlink and opening a new window). My book says now getWindowHandle() should return handle to PAGE2. However my program still returns handle to PAGE1.

Selenium v​​2.43

Selenium v2.43

可在Firefox和Chrome上重现。

Reproducible on Firefox and Chrome both.

问题:getWindowHandle()应返回的确切值是什么?

WebDriver wd = new ChromeDriver();
wd.get("file://D:/Projects/Selenium/Startup/web/ch3/switch_main.html");

String h1 = wd.getWindowHandle();// original handle
System.out.println("First handle = " + h1);

WebElement clickhere = wd.findElement(By.id("clickhere"));
clickhere.click();//moved to a new child page<

String h2 = wd.getWindowHandle();
System.out.println("Second handle = " + h2);// this handle is not different than h1


不同

推荐答案

getWindowHandle()将获取webDriver当前控制的页面句柄。此句柄是网页的唯一标识符。每次打开页面时都会有所不同,即使它是相同的URL。

getWindowHandle() will get the handle of the page the webDriver is currently controlling. This handle is a unique identifier for the web page. This is different every time you open a page even if it is the same URL.

getWindowHandles()(don '忘记's')会为你提供网页驱动程序理解的所有页面的所有句柄。请注意,当您将这些列表放入列表时,它们按照打开的顺序列出。

getWindowHandles() (don't forget the 's') will give you all the handles for all the pages that the web driver understands are open. Note that when you put these in a list they are listed in the order that they have been opened.

您可以使用 SwitchTo()。窗口(handle)切换到你想要的窗口。

You can use SwitchTo().Window("handle") to switch to the window you desire.

你可以使用 SwitchTo()。Window( mywindowID),如果您知道窗口ID。

You can use SwitchTo().Window("mywindowID"), if you know the window ID.

SwitchTo()。Window()将始终返回基本/主窗口。

SwitchTo().Window("") will always go back to the base/main window.

SwitchTo()。Frame(popupFrame)将转到来自webdriver当前控制的窗口的Popup。

SwitchTo().Frame("popupFrame") will get to the Popup that came from the window the webdriver is currently controlling.

这篇关于WebDriver.getWindowHandle()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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