Selenium C# - Opening New Tabs Becomes Slow or Times Out When Many Tabs Are Open

42 views
Skip to first unread message

Zubair Khakwani

unread,
Nov 27, 2024, 12:24:22 AM11/27/24
to Selenium Users
I am automating a website using Selenium in C#. My use case involves opening up to 50 tabs in a single browser instance, while also using multiple browser instances simultaneously in separate threads.

I have tested it in multiple systems with different requirenments.
1) Core i5-5th gen 8GB RAM - SSD
2) Core i7-7th gen 16GB RAM - SSD
3) Core i7-6th gen 32GB RAM - SSD

it works somewhat better on better machine but still cause the issue.

To open a new tab, I use the following command:
`var tab = Driver.SwitchTo().NewWindow(WindowType.Tab);`

This works fine initially, but as the number of tabs grows (around 50), the performance significantly degrades. Specifically:

 1. The command `Driver.SwitchTo().NewWindow(WindowType.Tab)` begins to take a long time (up to 30 seconds or more) just to open a new tab.
 2. In some cases, it throws a timeout exception after 60 seconds.
 3. When this happens, the new tab opens but remains in a "loading" state (as if a page is still loading) and is not automatically focused. If I manually click on the new tab in the browser, the command completes successfully.
My flow requires me to open a new tab, process the page, and then repeat for subsequent tabs. I cannot close a tab before opening another, so I must keep multiple tabs open at once.
 4. This issue increase if multiple browsers running in parallel.
 5. If in start of my program i open all tabs at once it also work quite better, it do get slow as tabs increase but like it take 3,4 seconds then, but if i open it one by one and process the page (which include filling a form and adding an attachment) it get's slow, If i am running one browser it still sometimes finish completely but with multiple browsers, most of the time it crashes due to timeout.
6) Also other commands will get slow like `Driver.WindowHandles` or `Driver.SwitchTo().Window(window)` or `Driver.CurrentWindowHandle` even this one `Driver.Url`

**What I Have Tried:**

 1. Workaround with Browser Extension: I installed a browser extension that listens for signals and opens tabs on command. This workaround works fine, but I have to open all tabs in start at once which i want to avoid and also I would prefer to avoid using extensions if possible.

 2. Investigated tab limits but found no clear guidance on why this issue arises at high tab counts.

**Questions**

 1. Why does opening a new tab slow down or timeout as the number of open tabs increases?
 2. Is there a way to optimize tab handling in Selenium to avoid these performance issues?
 3. Are there alternative approaches or configurations in Selenium to handle large numbers of tabs efficiently?


I am also considering to move to Playwright as i have heard it do not use WebDriver protocol to automate browser but rather use DevTools Protocol, but i am unsure exactly if it makes any difference or not. at this point

Adrian

unread,
Nov 27, 2024, 2:04:12 PM11/27/24
to Selenium Users
Hi,
Every time a new tab is added, then the memory usage also increases.
When those tabs are actively doing, things then this will use even more memory.
It sounds like your issue is related to the browser just using all the memory of the computer.  50 tabs is a lot of tabs and must be using a lot of memory!

Cheers,
Adrian.
Reply all
Reply to author
Forward
0 new messages