10
votes

I am using headless PhantomJS browser to automate the application using phantomjs driver with selenium. (selenium java version 3.5.2 and phantomjs.exe)

I have a scenario where i will fill the form and submit it and then the browser gets closed and after closing the browser I am reusing the driver reference to get the URL. It works well when I am using the firefox driver with selenium 2.47.0.

Now I switched to the selenium phontamjsdriver and phantombrowser. Here when I make a call to the driver.get(url);after the browser gets closed it is throwing the nosuchwindowexception saying window is closed or inactive. But, the same code is working with the firefox driver

example:

driver.get(url);// first time works  
submitForm(driver);//browser window gets closed.  
driver.get(url); 

The last line is throwing exception as:

nosuchwindowexception(selenium java with 3.5.2 version and phantomjs.exe). 

But works well with the firefoxbrowser with selenium 2.4.7.

2
How are you closing the browser , driver.quit() or driver.close() and are you also using the headless FF ? - cruisepandey
i am not closing the browser with phantom driver. its the behavir of the application once the form is submitted the broswer window gets closed automatically. It gets closed programatically in the code - Ramesh Roddam
AFAIU , you open one page in your application then click somewhere and a new windows opens up with form , then you fill the form and it gets closed by itself , then you use driver.get('some url') to navigate to some URL, right ? - cruisepandey
i open one page in application then click link. it opens the form on the same window and then i fill the form and it gets closed by itseft. then i use the driver.get("url") to open the browser with same driver instance. then it throws nosuch window exception. but the same code works well with firefox browser. looks like behaviour of phantom browser and firefox is not same - Ramesh Roddam
Are you using explicit wait ? Could you share your code too ? I need to see the code , before I can make any conclusion. - cruisepandey

2 Answers

0
votes

First of all, as you migrated from Selenium v2.47.0 to Selenium v3.5.2 it's worth to mention a lot have been changed with the availability of Selenium 3.x. Now Selenium-WebDriver is a W3C Recommendation Candidate and is compliant with WebDriver W3C Editor's Draft


NoSuchWindowException

NoSuchWindowException class extends NotFoundException and is majorly thrown while attempting:

WebDriver.switchTo().window(String windowName);

Now, a bit more details about your usecase, relevant HTML and your code block would have given us some more ideas what is going wrong. Perhaps the defination of submitForm(driver) holds the key to the solution of your question.


Best Practices

Here some of the best practices you can follow to avoid NoSuchWindowException:

  • Always strore the Parent Window Handle in a variable so that you can traverse back to the Parent Window as generally required.
  • Before invoking driver.switchTo().window(windowHandle); always induce WebDriverwait in-conjunction with ExpectedConditions method numberOfWindowsToBe(int).
  • Once you invoke driver.switchTo().window(windowHandle); induce WebDriverWait in-conjunction with ExpectedConditions method titleContains(java.lang.String title) to wait for the Page Loading to get completed to continue your Test Steps on the newly opened window.
  • To switch back to the parent window use the previously stored windowhandle.
  • Here is a sample code block to demonstrate Window/Tab handling:

    package demo;
    
    import java.util.Set;
    
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class WINDOW_HANDLE_ITERATE_Firefox 
    {
        public static void main(String[] args) throws Exception 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver =  new FirefoxDriver();
            driver.get("http://www.google.com");
            String parent_window = driver.getWindowHandle();
            System.out.println("Parent Window Handle is: "+driver.getWindowHandle());
            System.out.println("Page Title is: "+driver.getTitle());
            ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
            new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
            Set<String> allWindows_1 = driver.getWindowHandles();
            System.out.println("Total Windows: "+allWindows_1.size());
            for(String hand1:allWindows_1)
            if(!parent_window.equals(hand1))
            {
                driver.switchTo().window(hand1);
                new WebDriverWait(driver,10).until(ExpectedConditions.titleContains("Facebook"));
                String first_child_window = driver.getWindowHandle();
                System.out.println("First Child Window Handle is: "+first_child_window);
                System.out.println("First Child Window Page Title is: "+driver.getTitle());
                driver.close();
            }
            driver.switchTo().window(parent_window);
            System.out.println("Current Window Handle is : "+driver.getWindowHandle()+ " which is same as "+parent_window +", which is the parent window handle" );
            driver.quit();
        }
    }
    
  • Console Output:

    1531917836983   geckodriver INFO    geckodriver 0.20.1
    1531917836993   geckodriver INFO    Listening on 127.0.0.1:9993
    1531917837915   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\ATECHM~1\\AppData\\Local\\Temp\\rust_mozprofile.W5WqVulBNm9x"
    1531917842220   Marionette  INFO    Listening on port 35364
    1531917843126   Marionette  WARN    TLS certificate errors will be ignored for this session
    Jul 18, 2018 6:14:03 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Parent Window Handle is: 4294967297
    Page Title is: Google
    Total Windows: 2
    First Child Window Handle is: 4294967301
    First Child Window Page Title is: Facebook – log in or sign up
    Current Window Handle is : 4294967297 which is same as 4294967297, which is the parent window handle
    
0
votes

This may be an issue to bring to the PhantomJS team here https://github.com/ariya/phantomjs/issues

Unfortunately screenshots in Selenium only capture the DOM and not address bar. If you're able to save the pagesource, you may be able to extract the URL. When viewing page source of this page I see tags which list various 3rd party apps, inside there is a content source which lists the URL.

<meta name="twitter:app:url:googleplay" content="http://stackoverflow.com/questions/51255939/nosuchwindowexception-selenium-with-phantomjs-java">

This may not be true for every site, but could be somewhere to look. You could also try and add this tag in yourself if you're the site owner.