首页 > 解决方案 > 使用WinApp驱动在桌面任务栏中打开应用程序时如何切换应用程序

问题描述

我正在使用 Winapp 驱动程序、appium 和 c# 来自动化桌面应用程序。我的 scerios 是:我有一个应用程序,我单击打开 excel 表的按钮,该表现在显示在桌面的任务栏中。现在我想从我的桌面应用程序切换到 excel 工作表。

我无法找到相同的单一解决方案。帮助将不胜感激。

标签: c#appiumdesktopwinappdriver

解决方案


1.) 尝试通过以下方式获取 WindowHandles():

Set<String> windowHandles = driver.getWindowHandles();

如果这组有 2 个值,那么您可以通过以下方式轻松地在两个应用程序之间切换:

driver.switchTo.window(windowHandleOfExcel);

2.) 如果集合只包含一个句柄,那么您需要为您的 excel 创建一个新的桌面会话,您可以通过以下方式执行相同操作:

//get a root driver 
//all the opened application will be children of this root driver
//get the expected child driver

 DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("app", "Root");
        WindowsDriver rootDriver = new WindowsDriver(new URL("http://127.0.0.1:4723"), cap);


 WebElement ele = d.findElementByName("Book - Excel"); //Pass the header or title of the excel opened
        String toplevel = ele.getAttribute("NativeWindowHandle");
        int x  = (Integer.parseInt(toplevel));
        toplevel = Integer.toHexString(x);


DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("appTopLevelWindow", toplevel);
        WindowsDriver childdriver = new WindowsDriver(new URL("http://127.0.0.1:4723"), cap);

childDriver 是指向 Excel 应用程序的驱动程序。

希望这可以帮助。


推荐阅读