Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

java - WebDriver.getWindowHandle() method

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...