Skip to content
New issue

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

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

Already on GitHub? # to your account

[🐛 Bug]: Switch to window is not working after Chrome/Edge browser update version to 133 #15318

Open
AlokB1993 opened this issue Feb 21, 2025 · 9 comments

Comments

@AlokB1993
Copy link

AlokB1993 commented Feb 21, 2025

What happened?

Steps:

  1. User is in page 1
  2. user stores the window handle : var parentHandle= driver.windowHandle();
  3. Perform some event on the page new window opens
  4. Get all window handles: var allHandles= driver.windowHandles;
  5. Switch to the new window driver.switchTo().window(allHandles[1]);
  6. Perform save/close on the new window(New window closed)
  7. Try to switch back to parent window driver.switchTo().window(parentHandle);

Expected: Driver focus should go to parent window.
Actual: Its opening a new blank window and throwing the exception "No such window"

How can we reproduce the issue?

Steps:
1. User is in page 1
2. user stores the window handle : var parentHandle= driver.windowHandle();
3. Perform some event on the page new window opens
4. Get all window handles: var allHandles= driver.windowHandles;
5. Switch to the new window driver.switchTo().window(allHandles[1]);
6. Perform save/close on the new window(New window closed)
7. Try to switch back to parent window driver.switchTo().window(parentHandle);

Expected: Driver focus should go to parent window.
Actual: Its opening a new blank window and throwing the exception "No such window"

Relevant log output

Its opening a new blank window and throwing the exception "No such window"

Operating System

win10

Selenium version

c#, selenium 4.23

What are the browser(s) and version(s) where you see this issue?

Chrome 133 and Edge 133

What are the browser driver(s) and version(s) where you see this issue?

Selenium manager

Are you using Selenium Grid?

No

Copy link

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


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

@navin772
Copy link
Member

Hi @AlokB1993 , I can't reproduce this behaviour with both Selenium 4.23 and latest 4.29, if you could provide an example that reproduces the above behaviour for you, that would be great.

I am using the below script:

using System;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

class Program
{
    static void Main()
    {
        IWebDriver driver = new ChromeDriver();

        try
        {
            driver.Navigate().GoToUrl("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open");

            string parentHandle = driver.CurrentWindowHandle;

            // Click a button that opens a new window
            driver.SwitchTo().Frame("iframeResult");
            driver.FindElement(By.TagName("button")).Click();

            var allHandles = driver.WindowHandles;
            driver.SwitchTo().Window(allHandles[1]);

            Console.WriteLine("Title after switching: " + driver.Title);

            Thread.Sleep(1000);
            driver.Close();

            driver.SwitchTo().Window(parentHandle);
            Console.WriteLine("Title after switching back: " + driver.Title);
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception occurred: " + e.Message);
        }
        finally
        {
            driver.Quit();
        }
    }
}

And getting the output:

Title after switching: W3Schools Online Web Tutorials
Title after switching back: W3Schools Tryit Editor

which is correct and no exception is thrown.

@gianwehr
Copy link

Here's an issue on chromedriver that may relate to this. Since version 133, there's an issue where, if you have extensions installed or are opening a pdf, an additional window handle is generated.
https://issues.chromium.org/issues/396611138

@navin772
Copy link
Member

This is reproducible when opening a PDF in a new tab through the current page.

I see that 2 handles are being created for opening a single PDF which is resulting in this error -
unknown error: failed to close window in 20 seconds since it cannot close the "blank" handle.

So, in total 3 handles are present - 0, 1, 2 with (1) being the "blank" handle, if we switch to (2) handle - driver.switch_to.window(all_handles[2]) it works well.
This seems like a chromedriver issue since this was working well prior to v133.

@navin772
Copy link
Member

This works fine with Firefox, it generates only 1 handle when opening the PDF in new tab.

@AlokB1993
Copy link
Author

Hi @navin772, I agree with you. Even I have tried in my system with simple code for handling multiple window and it works. However in my project which is running from many years (it was working in older browser versions) it stopped working and when i downgrade the browser version to 132 there it's working fine. I will try to create a small repo for you how the things are happening there.

@AlokB1993
Copy link
Author

Hi @navin772 ,

I tried to get some sample code to clarify the issue much better for you. This is a working code till 132 browser version. please let us know if there is a new issue or there is any solution we need to update our code.

Session.Driver.GetDriver().WindowHandles; // Got 2 winids before saving the popup
SaveButton.Click(); //Working and new pop up closed
public override void WaitForClosed() // this method is waiting for the pop up to be closed
{
var v= Session.Driver.GetDriver().WindowHandles;
Wait
.With.Message($"Page is not closed: {Titles.FirstOrDefault()}; {Urls.FirstOrDefault()}")
.SetRetryInterval(SettingsService.TimeoutSettings.RetryTimeout.MultiplyBy(0.5))
.WaitUntil(() => !IsOpened());
}

public virtual bool IsOpened() // Session.Driver.ExecuteScript("return document.title;") code launching blank browser and i'm getting only 1 winid which is new
{
try
{
if (!Urls.Any() && !Titles.Any())
throw new FormatException("Page does not have Titles or Urls attribute to wait for.");

    // Use javascript instead of Driver properties to be able to handle frames
    var documentTitle = (string)Session.Driver.ExecuteScript("return document.title;");
    
    var documentUrl = (string)Session.Driver.ExecuteScript("return document.URL;");

    bool matchingTitleFound =
        Titles.Any(title => documentTitle.ToLower().Contains(title.ToLower()));
    bool matchingUrlFound =
        Urls.Any(url => documentUrl.ToLower().Contains(url.ToLower()));
    return matchingTitleFound && matchingUrlFound;
}
catch (NoSuchWindowException)
{
    return false;
}

}

@navin772
Copy link
Member

Hi @AlokB1993, as mentioned earlier, I think this is a chromedriver issue, let's wait for the fix.

@garrettgregor
Copy link

Any updates here or advice on which version of chromedriver we should use?

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

5 participants