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: `

JavaScript

Advertisement

Answer

This definitely might depend on opened tabs count.

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

JavaScript

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.

JavaScript

implement the mehtod addPdfUrlFromTheNewPage

JavaScript

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:

JavaScript
Advertisement