Exception in driver.switchTo().defaultContent() | Selenium Training|Java|WebDriver|TestNG|Frameworks|Videos
30% Mid August Discount Avail now

Selenium Training Forum

Exception in driver.switchTo().defaultContent()

S
Siddharth Rathod Post on 07/07/2019

Hi,

I am getting exception while switching to default window.

PFB the code and exception :-

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Naukri {

public static void main(String[] args) {

WebDriver driver = new ChromeDriver();

driver.get("http://www.globalsqa.com/demo-site/frames-and-windows/");
Set<String> s = new HashSet<String>();
s=driver.getWindowHandles();
System.out.println(s.size());
Iterator<String> t = s.iterator();
while (t.hasNext())
System.out.println(t.next());
driver.findElement(By.xpath("//div[@class='single_tab_div resp-tab-content resp-tab-content-active']/a")).click();
s=driver.getWindowHandles();
System.out.println(s.size());
t = s.iterator();
//String i ="1";

String first_window=t.next();
String second_window=t.next();
driver.switchTo().window(second_window);
driver.findElement(By.xpath("//*[@id='menu-item-1513']/a")).click();
driver.close();
driver.switchTo().defaultContent();
driver.findElement(By.xpath("//div[@class='single_tab_div resp-tab-content resp-tab-content-active']/a")).click();
}

}

 

 

Exception :-

Starting ChromeDriver 75.0.3770.90 (a6dcaf7e3ec6f70a194cc25e8149475c6590e025-refs/branch-heads/3770@{#1003}) on port 21669
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Jul 07, 2019 10:05:36 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
1
CDwindow-C7EAF21C9DD9FC2B4303562AC69FDC24
2
Exception in thread "main" org.openqa.selenium.NoSuchWindowException: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=75.0.3770.100)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'AA-PC', ip: '192.168.0.103', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_211'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.90 (a6dcaf7e3ec6f..., userDataDir: C:\Users\aa\AppData\Local\T...}, goog:chromeOptions: {debuggerAddress: localhost:61837}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: a21ceb5196cc0410a1efc6f624d0629e
*** Element info: {Using=xpath, value=//div[@class='single_tab_div resp-tab-content resp-tab-content-active']/a}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at Naukri.main(Naukri.java:42)

 

S

Instructor

Support Team Reply on 08/07/2019

You need to use the window id ind=stead of default content. default content is used in case of frames only.