Skip to content

[🐛 Bug]: driver.WindowHandles does not detect new tab opened by link or script in Selenium 4.26.0 (C#/.NET) #15703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shyampatadia opened this issue May 6, 2025 · 14 comments
Labels
A-needs-triaging A Selenium member will evaluate this soon! C-dotnet .NET Bindings D-chrome I-defect Something is not working as intended I-regression Something was working but we "fixed" it OS-windows

Comments

@shyampatadia
Copy link

shyampatadia commented May 6, 2025

Description


🐞 Bug Report: driver.WindowHandles does not detect new tab opened by link or script in Selenium 4.26.0 (C#/.NET)

Summary:
In Selenium WebDriver version 4.26.0 for .NET (C#), the driver.WindowHandles property intermittently fails to detect new browser tabs opened via anchor links (target="_blank") or JavaScript triggers. This behavior was not observed in previous versions and leads to NoSuchWindowException errors when switching to a newly opened tab using known working methods.


💻 Environment:

Component Version
Selenium WebDriver 4.26.0
Language Binding C# (.NET)
Browser Google Chrome (latest)
ChromeDriver Matches browser version
OS Windows 10 / 11

Expected Behavior:

After clicking a link that opens a new tab (e.g., target="_blank"), driver.WindowHandles should contain both the original and new tab window handles. Calling SwitchTo().Window() with the correct handle should allow switching between them.


Actual Behavior:

driver.WindowHandles continues to return only one handle, even after a second tab is visibly opened. This causes tab-switching logic based on partial window title or handle to fail.



Reproducible Code

Use the `driver.WindowHandles` method when 2 tabs are opened, on a browser, it returns me only one handle, and not 2.

ℹ️ Last known working version: 4.26.0

@shyampatadia shyampatadia added I-defect Something is not working as intended A-needs-triaging A Selenium member will evaluate this soon! labels May 6, 2025
@selenium-ci
Copy link
Member

@shyampatadia, thank you for creating this issue. We will troubleshoot it as soon as we can.

Selenium Triage Team: remember to follow the Triage Guide

@github-actions github-actions bot added C-dotnet .NET Bindings D-chrome I-regression Something was working but we "fixed" it OS-windows labels May 6, 2025
@shyampatadia
Copy link
Author

Closing this issue since similar issue was created earlier!

Link to the Previous issue

@AlokB1993
Copy link

Hi @selenium-ci , @shyampatadia, @navin772 , I'm still seeing the same issue on Chrome 136.0.7097.0 , as comments are disabled there (Not allowing me even if i have created the bug) #15703. May i know what is the exact resolution we are working on or do I need to create another bug on any other platform.

@shyampatadia
Copy link
Author

I dont think this issue is resolved yet, I closed the bug assuming that the issue wont be there for the chromdriver 136.0.7097.0, but when I actually tried it, the issue is still there, one of the engineer on chromedriver bug, mentioned that its resolved, but it has not been resolved, for now I am just using browserversion < 133.0

@navin772
Copy link
Member

Hi @AlokB1993, @shyampatadia can you share reproducible code for which you are facing the errors?
I had a script that opens a PDF in a new tab which was giving errors in earlier versions of chrome (v134) but now works perfectly with v136 after the chromedriver fix.

@AlokB1993
Copy link

AlokB1993 commented May 12, 2025

Hi @navin772 ,

In the code snippet, this method line no. 111 we are opening a new window and switching to that and before switching i have stored the parent window handle to scenario context. And in line number 120 from the code snippet i have the window handle of the parent page. after execution of line 119(Which closes the new window after clicking on add button) when i try to switch back to parent page, i get the exception No such window. Please find the attached screenshot.

Image

@navin772
Copy link
Member

@AlokB1993 I would need a minimal reproducible example (MRE) that I can run locally without the need of other dependent functions as in your code in order to find a resolution. Can you help me with that?
You can refer this for MRE.

@diemol
Copy link
Member

diemol commented May 12, 2025

@AlokB1993 that is not actionable at all. Screenshots give context but we need the actual code script to reproduce the issue.

@AlokB1993
Copy link

AlokB1993 commented May 12, 2025

Hi @navin772 , Unfortunately i can't provide you the code because this is an inhouse project, but i can summarize all the steps which will reproduce the bug.

  1. Parent page is opened.
  2. Click on something and a new page opened in another window.
  3. In the new page click accept button which closes the new window.
  4. Try to switch back to parent page with parent window handle and I'm getting the exception.

If you could find a dummy application which can replicate this functionality then it will be easy for us to reproduce the bug.

@diemol
Copy link
Member

diemol commented May 12, 2025

@AlokB1993 you do not need to share the exact same code. Any code that reproduces the issue is enough.

@navin772
Copy link
Member

@AlokB1993 I wrote custom HTML that did exactly what you said above (in those 4 steps) and it works perfectly fine.
As @diemol said, we would require a MRE from your side in order to help you.

@shyampatadia
Copy link
Author

using System;
using System.IO;
using System.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

class Program
{
    static void Main()
    {
        var pdfExtensionPath = @"C:\Path\To\PDFViewerExtension.crx"; 
        var downloadFolder = Path.Combine(Directory.GetCurrentDirectory(), "Downloads");

        var options = new ChromeOptions();
        options.AddExtension(pdfExtensionPath);
        options.AddArguments("--start-maximized", "--disable-infobars", "--ignore-certificate-errors");
        options.AddArguments("window-size=1600,900");
        options.AddAdditionalChromeOption("useAutomationExtension", false);
        options.AddExcludedArgument("enable-automation");
        options.AddUserProfilePreference("download.prompt_for_download", false);
        options.AddUserProfilePreference("download.default_directory", downloadFolder);
        options.AddUserProfilePreference("safebrowsing.enabled", false);

        using var driver = new ChromeDriver(options);
        driver.Navigate().GoToUrl("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");

      ((IJavaScriptExecutor)driver).ExecuteScript("window.open('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf', '_blank');");
  
        System.Threading.Thread.Sleep(2000); // Wait for new tab

        var handles = driver.WindowHandles;
        Console.WriteLine("Number of tabs open: " + handles.Count);

        if (handles.Count > 1)
        {
            driver.SwitchTo().Window(handles.Last());
            Console.WriteLine("Switched to new tab. URL: " + driver.Url);
        }
        else
        {
            Console.WriteLine("Selenium could not detect the new tab.");
        }

        Console.ReadKey();
    }
}

@shyampatadia
Copy link
Author

I think it does not work with the extension that I am loading, I mean you might be trying without pdf.js extension, I can reproduce this, with loading the pdf.js extension.

@navin772
Copy link
Member

@shyampatadia in that case, I think the issue is with the extension, not selenium. If you have a script that will reproduce the error without the extension, I can take a look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-needs-triaging A Selenium member will evaluate this soon! C-dotnet .NET Bindings D-chrome I-defect Something is not working as intended I-regression Something was working but we "fixed" it OS-windows
Projects
None yet
Development

No branches or pull requests

5 participants