Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Target window closed in selenium

In the attached screenshot,i want to click New Browser Window. I want to close the browser window and click on New Message window. I closed the browser window. But i am getting the exception

org.openqa.selenium.NoSuchWindowException: no such window: target window already closed

Screenshot

Below are the details Screenshot

Below is the code

@Test
public void testing()
{
    driver.manage().window().maximize();
    driver.get("http://www.seleniumframework.com/Practiceform/");
    driver.findElement(By.id("button1")).click();
    Set<String> handle=driver.getWindowHandles();
    for(String handles:handle){
       try{
          String text=driver.switchTo().window(handles).getPageSource();
          if(text.contains("Agile Testing and ATDD Automation")){
              System.out.println("Text found");
              driver.close();
              break;
          }
       }catch(Exception e){}
    }
    driver.switchTo().defaultContent();
    driver.findElement(By.xpath("//button[contains(text(),'New Message Window')]")).click();
    driver.quit();
like image 637
Manish B Avatar asked Sep 16 '16 12:09

Manish B


People also ask

Which option closes the newly opened window in selenium?

close() closes the original window or tab, not the new one.

What is window () in selenium?

What is a window in Selenium? A window in any browser is the main webpage on which the user is landed after hitting a link/URL. Such a window in Selenium is referred to as the parent window also known as the main window which opens when the Selenium WebDriver session is created and has all the focus of the WebDriver.

How do I resolve no such windows exception?

To overcome this, we need to handle the opened windows using one of the webdriver methods called “driver. getWindowHandles()”. Once you have this information then you can switch to the corresponding window to automate or interact with the window.

How to switch to the target window in Selenium WebDriver?

– driver.switchTo ().window (String handle) – We can switch to the target window by using its handle. Example-1: How to Handle Multiple Windows in Selenium?

How to handle windows in selenium?

Get window position and write it to the console. Set window position x=100 and y=200 and write to the console. You learned how to handle windows in selenium and managing windows such as selenium maximize window, size, and position functions. Onur Baskirt is a senior IT professional with 15+ years of experience.

How to maximize window size and position in selenium?

Minimize the window by 1/4 and write the new screen size to the console. Get window position and write it to the console. Set window position x=100 and y=200 and write to the console. You learned how to handle windows in selenium and managing windows such as selenium maximize window, size, and position functions.

What is prompt alert in Selenium WebDriver?

This Prompt Alert asks some input from the user and Selenium webdriver can enter the text using sendkeys (” input…. “). 3) Confirmation Alert. This confirmation alert asks permission to do some type of operation. Alert interface provides the below few methods which are widely used in Selenium Webdriver.


1 Answers

I guess you try to back to original window using driver.switchTo().defaultContent(); ? This is not proper way.

You should:

  1. store the original window at the beginning of test:

String winHandleBefore = driver.getWindowHandle();

  1. click button, switch windows, do whatever

  2. To back to original window use:

driver.switchTo().window(winHandleBefore);

Answer based on:https://stackoverflow.com/a/9597714/4855333

like image 158
kotoj Avatar answered Oct 24 '22 05:10

kotoj