Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

How to handle pop-up window using Selenium? ("driver.switchTo().window(winHandle);" not working consistently)

We are facing some issue in switching to new window after clicking on the link/button

I was using the below code for switching to new window.

elem.click();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle); }

Observed that scripts runs inconsistently, It will be great if i get some consistent code for switching to new window. I feel there is a synchronization issue while switching to new window.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

If @HelpingHands suggestion will not work the problem may lies in the several handlers exist simultaneously The following code may help: When you create the driver please save the appropriated handler

driver = new FirefoxDriver(profile);
driver.get(uri);
MainWinHandler = driver.getWindowHandle();

Then in order to switch

for(String winHandle : driver.getWindowHandles()){
    if(!winHandle.equals(MainWinHandler))
        driver.switchTo().window(winHandle);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...