NoSuchWindowException - Selenium 3.4.0 - IE 11

433 views Asked by At

I have launched IE 11 browser,

I have navigated to a initial URL --> done mouse over and clicked a link --> it redirects to another page.

In that page, I have to click a button, but it is throwing an exception

org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window

but the windows still available on screen.

This is my code

WebElement e = driver.findElement(By.xpath("html/body/div[2]/div/div/header/nav/ul/li[2]/a"));

    Actions a = new Actions(driver);
    a.moveToElement(e).build().perform();

    driver.findElement(By.xpath("//*[@id='menu-item-35']/a")).click();



  TimeUnit.SECONDS.sleep(5);

// Exception is occurs after this, but when I delete the below code, the test case passes

driver.findElement(By.xpath("//*[@id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input")).click();

This is the URL of the page: http://store.demoqa.com/

2

There are 2 answers

1
talkingfox On

It looks to me like this is a race-condition error. I had those cases myself where I could actually see a window, but Selenium still said it wasn't there.

Have you tried setting the sleep time to a higher value?

You could also try to put a expect-wait-condition before clicking your element.

    WebDriverWait wait = new WebDriverWait(DRIVER, 20);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input"))
1
undetected Selenium On

Here is the Solution to your Question:

A few words about the solution:

  1. The xpath html/body/div[2]/div/div/header/nav/ul/li[2]/a looks petty vulnerable to me use linkText locator.
  2. Once you use Action Class to build().perform(), induce a bit of wait before locating another element.
  3. Instead of xpath locator of //*[@id='menu-item-35']/a element, use linkText locator.
  4. Again the xpath //*[@id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input looks petty vulnerable to me use a logical uniquexpath.
  5. Here is your own working code block with some simple tweaks in it:

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    WebElement e = driver.findElement(By.linkText("Product Category"));
    Actions a = new Actions(driver);
    a.moveToElement(e).build().perform();
    driver.findElement(By.linkText("iMacs")).click();
    driver.findElement(By.xpath("//input[@name='Buy']")).click();
    

Let me know if this Answers your Question.