Skip to content
Advertisement

Selenium And Java: Exception in thread “main” org.openqa.selenium.NoSuchWindowException: no such window: target window already closed

I am accessing a Quebec Laws website and I am trying to web scrape all of its law names along with their associated PDFs. When doing this, I open each and every tab of each law and then go through all those tabs to get the information I am looking for. However, after a while of going through the tabs I get the following error: “Exception in thread “main” org.openqa.selenium.NoSuchWindowException: no such window: target window already closed “. I am unsure why this is popping up. I believe it’s because the number of tabs is so long as the same code I used for a smaller number of tabs worked fine. Here is my code: `

System.setProperty("webdriver.chrome.driver", "C:\WorkSpace\Driver\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5000));
        driver.manage().window().maximize();
        driver.get("http://www.legisquebec.gouv.qc.ca/en/chapters?corpus=regs&selection=all");
        wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector("tr.clickable a"), 100));
        Thread.sleep(50);

        List<WebElement> QuebecConsolidatedRegulations = driver.findElements(By.cssSelector("tr.clickable a"));
        String parent = driver.getWindowHandle();
        for (int i=0; i<QuebecConsolidatedRegulations.size(); i++){
            String opentabs = Keys.chord(Keys.CONTROL, Keys.ENTER);
            
            ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", QuebecConsolidatedRegulations.get(i));
            Thread.sleep(300);
            wait.until(ExpectedConditions.visibilityOf(QuebecConsolidatedRegulations.get(i)));
            QuebecConsolidatedRegulations.get(i).sendKeys(opentabs);
        }

        int i=0; 
            Set<String> tabs = driver.getWindowHandles();
            for (String child:tabs){
                // try{
                    if (!parent.equalsIgnoreCase(child)){
                        driver.close();
                        
                        driver.switchTo().window(child);
                        String StatuteName = driver.findElement(By.xpath("//*[@id='form']/div[2]/div[1]/h3")).getText();
                        String pagePdfUrl = driver.findElement(By.xpath("//*[@id='renditionPdf']")).getAttribute("href");
                        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
                        if (i<QuebecConsolidatedRegulations.size()){
                            ConsolidatedRegulationsAndPDFs.put(StatuteName, pagePdfUrl);
                            i+=1;
                        }
                        else{
                            continue;
                        }
                    }
               // }
                // catch(NoSuchWindowException e){
                //     continue;
                // }
                
               
                    
            }
            return ConsolidatedRegulationsAndPDFs;
    }`

Advertisement

Answer

This definitely might depend on opened tabs count.

I’ve done some research with the next script (groovy):

static void main(a) {
    WebDriverManager.chromedriver().setup()
    WebDriver driver = new ChromeDriver(new ChromeOptions())
    driver.get('https://nbc.com')
    300.times {
        driver.executeScript("window.open('https://nbc.com')")
        println('windows size:' + driver.getWindowHandles().size())
        driver.switchTo().window(driver.getWindowHandles().first())
        driver.switchTo().window(driver.getWindowHandles().last())
    }
    driver.quit()
}

And it fails with

Exception in thread "main" org.openqa.selenium.WebDriverException: chrome not reachable

after 92 new tabs opened (note: I have 16GB RAM). Tabs count before fail depends on the site and the memory, it consuming..

And on your page there are 3000+ QuebecConsolidatedRegulations items.


I suggest do not keep so many tabs opened

I suggesting in a loop:

  • open a new tab,
  • collect data,
  • close the tab
  • switch to the first window

In this way you’ll have 1 or 2 tabs opened simultaneously and not more.

List<WebElement> QuebecConsolidatedRegulations = driver.findElements(By.cssSelector("tr.clickable a"));
String parent = driver.getWindowHandle();
for (int i=0; i<QuebecConsolidatedRegulations.size(); i++){
    String opentabs = Keys.chord(Keys.CONTROL, Keys.ENTER);
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", QuebecConsolidatedRegulations.get(i));
    Thread.sleep(300);
wait.until(ExpectedConditions.visibilityOf(QuebecConsolidatedRegulations.get(i)));
    QuebecConsolidatedRegulations.get(i).sendKeys(opentabs);
    // do the action within a loop but in new window
    addPdfUrlFromTheNewPage(driver, ConsolidatedRegulationsAndPDFs);
}

implement the mehtod addPdfUrlFromTheNewPage

public static void addPdfUrlFromTheNewPage(WebDriver driver, Map resultsMap) {
    List<String> tabs = new ArrayList<>(driver.getWindowHandles());
    String lastTab = tabs.get(tabs.size()-1);
    driver.switchTo().window(lastTab);
    String StatuteName = driver.findElement(By.xpath("//*[@id='form']/div[2]/div[1]/h3")).getText();
    String pagePdfUrl = driver.findElement(By.xpath("//*[@id='renditionPdf']")).getAttribute("href");
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    resultsMap.put(StatuteName, pagePdfUrl);
    driver.close(); //close current window
    driver.switchTo().window(tabs.get(0));  //switch to initial window
}

It had worked more than 20 minutes and I think 1500+ items were scraped. Then I just stopped execution. I think it will work without any issues.

output:

2018C23, r. 1 - Regulation respecting certain transitional measures for the carrying out of the Act mainly to improve the regulation of the financial sector, the protection of deposits of money and the operation of financial institutions=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/2018C23,%20R.%201.pdf
A-2.02, r. 1 - Regulation respecting the application of the Act to promote access to justice through the establishment of the Service administratif de rajustement des pensions alimentaires pour enfants=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-2.02,%20R.%201.pdf
A-2.1, r. 1 - Code of ethics of the members of the Commission d’accès à l’information=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-2.1,%20R.%201.pdf
A-2.1, r. 2 - Regulation respecting the distribution of information and the protection of personal information=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-2.1,%20R.%202.pdf
A-2.1, r. 3 - Regulation respecting fees for the transcription, reproduction or transmission of documents or personal information=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-2.1,%20R.%203.pdf
A-2.1, r. 4 - Regulation respecting public bodies that must refuse to release or to confirm the existence of certain information=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-2.1,%20R.%204.pdf
A-2.1, r. 5 - Regulation respecting the procedure for selecting persons qualified for appointment as members of the Commission d’accès à l’information=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-2.1,%20R.%205.pdf
A-2.1, r. 6 - Rules of Proof and Procedure before the Commission d’accès à l’information=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-2.1,%20R.%206.pdf
A-3, r. 1 - Regulation respecting financial assistance=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-3,%20R.%201.pdf
A-3, r. 2 - Regulation respecting the impairment scale=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-3,%20R.%202.pdf
A-3, r. 3 - Regulation respecting payment of expenses for organizing and maintaining rescue stations in mines by the Commission des normes, de l’équité, de la santé et de la sécurité du travail, and the reimbursement by interested employers of sums disbursed=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-3,%20R.%203.pdf
A-3, r. 4 - Regulation respecting reimbursement of damaged or destroyed clothing, prosthesis or orthesis=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-3,%20R.%204.pdf
A-3, r. 5 - Regulation respecting the transportation of the body of a worker=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-3,%20R.%205.pdf
A-3.001, r. 1 - Regulation respecting medical aid=http://www.legisquebec.gouv.qc.ca/en/pdf/cr/A-3.001,%20R.%201.pdf

... +3000 lines
Advertisement