WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

Can't switch from application to desktop

Open icnocop opened this issue 7 years ago • 6 comments

Hi.

When I try to switch from an application window to the desktop window, I get the following error: OpenQA.Selenium.WebDriverException: 10010 is not a top level window handle

    [TestClass]
    public class UnitTest1
    {
        Uri winAppDriverUri = new Uri("http://127.0.0.1:4723/wd/hub");

        Process driverProcess = null;

        [TestInitialize]
        public void TestInitialize()
        {
            driverProcess = LaunchDriver(winAppDriverUri);
        }

        [TestCleanup]
        public void TestCleanup()
        {
            driverProcess?.Close();
        }

        [TestMethod]
        public void AppRoot() // this works and outputs the same window handle as the next test method
        {
            AppiumOptions options = new AppiumOptions();
            options.PlatformName = "Windows";
            options.AddAdditionalCapability("platformVersion", "1.0");
            options.AddAdditionalCapability("app", "Root");

            WindowsDriver<WindowsElement> driver = new WindowsDriver<WindowsElement>(winAppDriverUri, options);
            Console.WriteLine(driver.CurrentWindowHandle);
        }

        [TestMethod]
        public void SwitchToDesktop() // this fails with WebDriverException: 10010 is not a top level window handle
        {
            AppiumOptions options = new AppiumOptions();
            options.PlatformName = "Windows";
            options.AddAdditionalCapability("platformVersion", "1.0");
            options.AddAdditionalCapability("app", @"C:\Windows\System32\notepad.exe");

            WindowsDriver<WindowsElement> driver = new WindowsDriver<WindowsElement>(winAppDriverUri, options);

            IntPtr desktopWindowHandle = GetDesktopWindow();

            string desktopWindowHandleAsHex = desktopWindowHandle.ToInt32().ToString("x");

            Console.WriteLine(desktopWindowHandleAsHex);

            driver.SwitchTo().Window(desktopWindowHandleAsHex);
        }

        private Process LaunchDriver(Uri uri)
        {
            string exeFilePath = @"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe";
            string commandLineArguments = $"{uri.Host} {uri.Port}{uri.PathAndQuery}";

            ProcessStartInfo processStartInfo = new ProcessStartInfo(
                exeFilePath,
                commandLineArguments)
            {
                CreateNoWindow = true,
                UseShellExecute = false
            };

            Process process = new Process()
            {
                StartInfo = processStartInfo
            };

            process.Start();

            return process;
        }

        [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
        private static extern IntPtr GetDesktopWindow();
    }

Any ideas?

Thank you.

icnocop avatar May 16 '19 23:05 icnocop

I think it's the "GetDesktopWindow();" call that's probably causing the issue. You should identify an attribute in your app and find the Window handle that way.

Example:

 var mainWindow = DesktopSession.FindElementByName("CalculatorWindow");
               var mainWindowHandle = mainWindow.GetAttribute("NativeWindowHandle");
               mainWindowHandle = (int.Parse(mainWindowHandle)).ToString("x"); // Convert to Hex

Please reopen if you're still facing this issue or have any other questions.

hassanuz avatar Nov 01 '19 21:11 hassanuz

I am facing same issue and not able to resolve with above suggestion.

My case is : I have an application window, after clicking on button it opens next window which is a part of application window. We can not consider it as top level window. So how it will be resolved.

I wrote below code:

var PartofMainWindow = driver1.FindElementByName("NameOfMainWindow");
           var NonTopLevelWindowHandle = PartofMainWindow.GetAttribute("NativeWindowHandle");
           NonTopLevelWindowHandle = (int.Parse(NonTopLevelWindowHandle)).ToString("x"); // Convert to Hex

           AppiumOptions desired = new AppiumOptions();
           desired.AddAdditionalCapability("appTopLevelWindow", TopLevelWindowHandle);
           WindowsDriver<WindowsElement>  driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), desired);


It shows error OpenQA.Selenium.WebDriverException: 1e5245 is not a top level window handle

and When i tried below code:

var PartofMainWindow = driver1.FindElementByName("NameOfMainWindow");
            var NonTopLevelWindowHandle = PartofMainWindow.GetAttribute("NativeWindowHandle");
            NonTopLevelWindowHandle = (int.Parse(NonTopLevelWindowHandle)).ToString("x"); 


driver.SwitchTo().Window(NonTopLevelWindowHandle);

then it is showing error OpenQA.Selenium.WebDriverException: 'An unknown error occurred in the remote end while processing the command.'

both code is not working for me.

Neh18 avatar May 06 '20 04:05 Neh18

I'm disappointed this was closed without an answer. I'm seeing the same issue.

TheHLee avatar May 20 '20 00:05 TheHLee

I got the same issue. not sure if there is no way to trace from a WindowElement up to the top level window. my problem is the main window has no ID, its name changes including signed user, Call Name also changes, it looks like to have 4 parts, the last part changes. ClassName: "Afx:00400000:8:00010003:00000000:011E051D" the question:

  1. can I search by name but with a partial match like "name%". ?
  2. can I search by class name with a partial match ?
  3. If I can locate a child window, how can I trace up the top level ?

thanks and regrds

henry1999sg avatar May 29 '20 04:05 henry1999sg

Why they did not bother to answer the question Neh18 asked! Really disappointed for that. The whole internet is asking for the same problem that they are not able to switch to a top level window. But none, literally none is asking how to switch to a NonTopLevelWindow. I'm stuck on this issue and miraculously there seems to be no solution. Awesome!

BTW, Neh18, TheHlee did you guys find any solution for this problem?

Shabbir-Hossain avatar Jun 24 '20 04:06 Shabbir-Hossain

Just found the solution of this problem. I'm just disgusted after knowing how easy the solution is!!!!

I posted it on stackOverflow. You can check it out here

Shabbir-Hossain avatar Jun 24 '20 07:06 Shabbir-Hossain