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();
close() closes the original window or tab, not the new one.
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.
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.
– 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?
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.
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.
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.
I guess you try to back to original window using driver.switchTo().defaultContent();
? This is not proper way.
You should:
String winHandleBefore = driver.getWindowHandle();
click button, switch windows, do whatever
To back to original window use:
driver.switchTo().window(winHandleBefore);
Answer based on:https://stackoverflow.com/a/9597714/4855333
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With