Failing to focus on parent window in webdriver

862 views
Skip to first unread message

Naroop

unread,
Mar 22, 2015, 12:00:13 AM3/22/15
to seleniu...@googlegroups.com
Hi All, 

I am facing issues when handling parent an child windows with selenium webdriver, 


Able to switch over to child window and performed actions on child window. After performing operations in child window by clicking on "OK" button child window automatically closing. Here the problem occurring. Parent window not recognizing. 


If the new window closed automatically after clicking save button on new window then i am unable to shift webdriver focus to main window.

                                Set<String> windows = driver.getWindowHandles(); 
               String parent = null; 
               String child = null; 
               Iterator<String> it = windows.iterator(); 
               while(it.hasNext()) 
               { 
               parent = (String) it.next(); 
               child = (String) it.next(); 
               } 
               driver.switchTo().window(child); 
                                 
               driver.findElement(By.xpath("//div[@id='x-auto-32__x-auto-32_x-auto-137']/img[2]")).click();Thread.sleep(1000);
               driver.findElement(By.xpath("//div[@id='x-auto-32__x-auto-32_x-auto-141']/span[2]")).click();Thread.sleep(1000);
               driver.findElement(By.cssSelector("div.x-grid3-cell-inner.x-grid3-col-runStatus")).click();Thread.sleep(1000);
               driver.findElement(By.xpath("//div[@id='x-auto-36']")).click();Thread.sleep(1000);
               driver.findElement(By.xpath("(//button[@type='button'])[11]")).click();Thread.sleep(1000);
               
               driver.findElement(By.id("mainForm:addinterface")).click();  
                driver.close();  
               

                               // till this part working fine.          
               
                driver.switchTo().window(parent); // Here failing to focus on parent window and stopped to perform actions on parent window. 
                
               driver.findElement(By.id("mainForm:add")).click();
               
               TestLogger.info("Get URL"+ driver.getCurrentUrl());
               TestLogger.info("Get Window Title"+ driver.getWindowHandle());
               TestLogger.info("Get URL"+ driver.getPageSource());

                Can somebody help here to proceed ? 


Regards,
Naroop                







PeterJeffreyGale

unread,
Mar 22, 2015, 5:34:24 AM3/22/15
to seleniu...@googlegroups.com
There is no guarantee that the parent window is the first one in your list, as you logic assumes.

Iterate over the full list of window handles and assign the assign the window handles only if the window title matches what you expect it to be.

Xiang Dong

unread,
Mar 22, 2015, 7:01:54 AM3/22/15
to seleniu...@googlegroups.com
remove driver.close and have a try, I had a similar case, click a button child window will be closed, switch back to parent window has no problem. if it does not work, you can try delegate the click action to a javascript setitimeout method, give your test case an opportunity to switch back to parent window before child window get closed.

Best Regards,
--david


Date: Sat, 21 Mar 2015 07:36:54 -0700
From: gandham.r...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Failing to focus on parent window in webdriver
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/b1b5b004-e6b8-4249-9b37-8399b7b4e8dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mukesh otwani

unread,
Mar 22, 2015, 3:34:25 PM3/22/15
to seleniu...@googlegroups.com
Hi Naroop,

You should getparent window id first.. In your case it is null.Refer below code and let me know


Naroop

unread,
Mar 23, 2015, 3:27:11 AM3/23/15
to seleniu...@googlegroups.com
Hi All, 

Thanks for your responses. 

The issue solved by removing driver.close() as per David's suggestion. Since child window automatically closing after performing operations, then driver.close() is meaningless in my case. 

Thanks David. 

Regards,
Naroop.
Reply all
Reply to author
Forward
0 new messages