We Driver: Unable to switch back to the parent window from child

831 views
Skip to first unread message

DAYANANDA REDDY CHALLA

unread,
Sep 2, 2012, 12:37:14 AM9/2/12
to seleniu...@googlegroups.com
Hi,

I have a scenario where a new browser window opens when a button is clicked. I need to close the new window after checking the elements and switch back to parent window.

I tried the following code but I am unable to close the child window because of which I am unable to get the control back to the parent window.

Could you please help me on this issue? 

package testDriver;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.SeleneseTestNgHelper;

public class Driver extends SeleneseTestNgHelper
{
private WebDriver driver = null;
private String parentwindow;
@BeforeClass
public void setup() throws InterruptedException
{
//driver = new FirefoxDriver();
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void user() throws InterruptedException
{
driver.findElement(By.xpath("//input[@name='username']")).sendKeys("ccaadmin");
driver.findElement(By.xpath("//input[@name='password']")).sendKeys("ccaadmin");
driver.findElement(By.xpath("//input[@name='password']/following::div[text()='Login']")).click();
Thread.sleep(2000);
parentwindow = driver.getWindowHandle();
driver.findElement(By.xpath("//td[text()='Services']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//td[text()='Reports']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//div[contains(text(),'Activity Log')]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//td[contains(text(),'Filters')]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//div[text()='Run/View']")).click();
Thread.sleep(50000);
//Switch to new window opened
        for(String winHandle : driver.getWindowHandles())
        {
            driver.switchTo().window(winHandle);
        }

        // Perform the actions on new window
driver.switchTo().frame("reportframe");
driver.switchTo().frame("openDocChildFrame");
Thread.sleep(2000);
driver.findElement(By.id("IconImg_CrystalReportViewer_toptoolbar_export")).click();
Thread.sleep(20000);
//Close the new window, if that window no more required
   driver.close();
   
   driver.switchTo().window(parentwindow);
}
@AfterTest
public void tearDown() throws InterruptedException
{
driver.switchTo().window(parentwindow);
System.out.println("aftertest");
driver.findElement(By.xpath("//html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr/td[3]/table/tbody/tr/td[2]/div/div")).click();
Thread.sleep(2000);
System.out.println("after click onlogout");
driver.quit();
}
}


--

Thanks & Regards,

DAYANANDA REDDY CHALLA


-----------------------------------------------------------------------

The sooner you fall behind, the more time you'll have to catch up.

http://nandu310.wordpress.com


Santosh Hulisandra Srikanta

unread,
Sep 2, 2012, 9:02:00 AM9/2/12
to seleniu...@googlegroups.com
before calling    driver.switchTo().window(parentwindow);  you should call    driver.switchto.defaultcontent .

Since you are into an Iframe in the new window.

/santosh 

On Sun, Sep 2, 2012 at 10:47 AM, murali <mural...@gmail.com> wrote:
Hi,

Do you need to switch back to parent window, driver.switchto.defaultcontent also works good for me without closing another popup.

After closing popup, switchback mostly not work i think because popup is not present to switch from it.,i guess not sure... so cursor is at active window after closing means on main window. so switchto.active works...

kindly reply me to know what exact works here...

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/wiDbj-LK-yEJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

/Santosh

Popsha

unread,
Sep 2, 2012, 8:29:00 PM9/2/12
to seleniu...@googlegroups.com
What error do you get?

priya

unread,
Oct 9, 2014, 8:14:45 AM10/9/14
to seleniu...@googlegroups.com
Hi,
I did same .. switch back to parent window ,driver.switchto.defaultcontent and also i tried with switchto.activeelement 
but its not working..

Actually want to switch back to Iframe parent window.. 
Reply all
Reply to author
Forward
0 new messages