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) Let's say I am on page PAGE1. So getWindowHandle() should return handle to PAGE1. (Correct)
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 v2.43
Reproducible on Firefox and Chrome both.
Question: What is the exact value that getWindowHandle() should return?
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
The getWindowHandles method is used to store all the opened window handles in the Set data structure. The getWindowHandle method is used to store the window handle of the browser window in focus.
Return type of getWindowHandle() is String while return type of getWindowHandles() is Set<String>. The return type is Set as window handle is always unique. In chrome and Firefox , Each tab in a window will have unique window handles. So getWindowHandles() will return handles for all tabs of a window.
Get the handles of all the windows that are currently open using the command: Set<String> allWindowHandles = driver. getWindowHandles(); which returns the set of handles. Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.
The window handle is a unique identifier that stores the values of windows opened on a webpage and helps in window handling in Selenium. getWindowHandles( ) and getWindowHandles( ) handle windows in Selenium. The user has to switch from the parent window to the child window to work on them using switchTo( ) method.
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'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.
You can use SwitchTo().Window("handle")
to switch to the window you desire.
You can use SwitchTo().Window("mywindowID")
, if you know the window ID.
SwitchTo().Window("")
will always go back to the base/main window.
SwitchTo().Frame("popupFrame")
will get to the Popup that came from the window the webdriver is currently controlling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With