Window Handles list is empty
See original GitHub issueI’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:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
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.
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 😃