question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Window Handles list is empty

See original GitHub issue

I’m trying to switch between windows using the ‘switchto’ method, which requires a window handle. When I try to get the list of window handles, I always get an empty list. The code below shows this. The code below also lists the titles of all the windows, indicating that there seems to be some disconnect.

Any thoughts?

import io.appium.java_client.windows.WindowsDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;

public class GetWindowHandles {
    public static void main (String[] args) {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("app", "Root");
        WindowsDriver DesktopSession;
        try {
            DesktopSession = new WindowsDriver(new URL("http://127.0.0.1:5678"), capabilities);
            System.out.println("Window handles: " + DesktopSession.getWindowHandles());
            java.util.List elements = DesktopSession.findElementsByXPath("/Pane/Window");
            String titleText="";
            for (Object element : elements){
                titleText = ((WebElement)element).getText();
                System.out.println(titleText);
            }
            DesktopSession.switchTo().window("c:\\WINDOWS\\System32\\cmd.exe");
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

May 30, 2019 12:32:24 PM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0 INFO: Detected dialect: OSS Window handles: [] C:\WINDOWS\System32\cmd.exe C:\WINDOWS\System32\cmd.exe Error: An unknown error occurred in the remote end while processing the command. (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds Build info: version: ‘3.12.0’, revision: ‘7c6e0b3’, time: ‘2018-05-08T14:04:26.12Z’ System info: host: ‘S7996’, ip: ‘192.168.1.141’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0_201’ Driver info: io.appium.java_client.windows.WindowsDriver Capabilities {app: Root, javascriptEnabled: true, platform: WINDOWS, platformName: WINDOWS} Session ID: B356EC4A-25F2-4A87-AB42-685428FA3434

Process finished with exit code 0

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
timotiusmargocommented, Jun 13, 2019

Hi @robertwatkins,

By definition, the windows_handles command in JSON wire protocol “[retrieves] the list of all window handles available to the session”. In a typical multiple windows application such as Microsoft Edge, this command will return all the opened Microsoft Edge windows at that particular time. All of these windows have the same process Id and are available to switch unto from the Microsoft Edge session.

In your sample code, you attempted to retrieve all window handles belonging to the root desktop pane which is indeed empty as there are no top level windows with that shares the same process Id. Instead you could call FindElementsByClassName to enumerate all ‘Window’ elements as shown below:

var windows = session.FindElementsByClassName("Window");

Note that the command above will also include non top level windows. You will need to use XPath elements search instead if you only want to get the top level windows only.

In summary, I can see how much easier it would be if the GetWindowHandles command return all top level windows under root desktop UI tree. This should be discussed and can potentially be implemented if everyone agrees toward this direction.

1reaction
robertwatkinscommented, Jun 11, 2019

That’s unfortunate. I’m not clear as to why that limitation is necessary since that information is essentially available. However, I do see your point 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Switching to empty window handle (Selenium webdriver)
Two questions arises here: 1- what is an empty window handle u''? 2- Did I switch to u'' and failed and switched back...
Read more >
Solved: Getting the list of Open Window Handles in C# ...
This works fine using Process.GetProcesses(), and using the MainWindowTitle property to see if its not empty. This however does not work for an ......
Read more >
Working with windows and tabs
Each window has a unique identifier which remains persistent in a single session. You can get the window handle of the current window...
Read more >
How to list all Wine window handles which belong to the ...
Here is the command listing the selected handles (replace terminal.exe with the app name):. $ winedbg --command "info wnd" | grep -wf ...
Read more >
How To Handle NoSuchWindowException In Selenium
Get a list of all window handles and print them on the console. Try to switch to the new window using an invalid...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found