【问题标题】:winappdriver couldn't find element usingwinappdriver 找不到使用的元素
【发布时间】:2020-02-13 03:44:00
【问题描述】:

我是自动化新手,我正在尝试使用带有 C# 的 WinAppDriver 来自动化 WPF 应用程序。我能够加载应用程序,但在尝试使用给定的搜索参数查找具有 Name/AccessibilityId 的元素时收到类似 {"An element could not be located on the page using the given search parameters."} 之类的错误,即使在保持等待时间后也是如此。

见下文:

POST /session/09551C9F-CF20-4C2B-A900-F17D2483F9D8/element HTTP/1.1
Accept: application/json, image/png
Content-Length: 45
Content-Type: application/json;charset=utf-8
Host: 127.0.0.1:4723

{"using":"accessibility id","value":"TxtPwd"}
HTTP/1.1 404 Not Found
Content-Length: 139
Content-Type: application/json

{"status":7,"value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters."}}

我不知道发生了什么。有什么建议吗?

我确实喜欢 - 通过检查工具检查元素和元素的自动化 ID/名称 - 设置开发者模式激活 - 找到元素之前的等待时间

 var aDesiredCapabilities = new DesiredCapabilities();
             aDesiredCapabilities.SetCapability("app", @"PathToApplication");
             aDesiredCapabilities.SetCapability("deviceName", "Windows 10");

             var aWindow = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), aDesiredCapabilities);
             aWindow.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

             aWindow.FindElementByAccessibilityId("TxtPwd").SendKeys("qwerty");
             aWindow.FindElementByAccessibilityId("TxtUser").SendKeys("123456");
             aWindow.FindElementByAccessibilityId("Clear").Click();

             aWindow.FindElementByAccessibilityId("TxtPwd").SendKeys("qwerty");
             aWindow.FindElementByAccessibilityId("TxtUser").SendKeys("123456");
             aWindow.FindElementByAccessibilityId("Login");

【问题讨论】:

  • 您尝试过这些不同的方法吗? github.com/microsoft/WinAppDriver/wiki/…
  • 我尝试使用 FindElementByAccessibilityId 和 FindElementByName。其他我没有。我希望它至少应该与 FindElementByAccessibilityId 一起使用。
  • 代替sendkeys,尝试使用click()函数点击元素。首先查看您的元素是可交互的。 element.enabled == true,element.displayed == true。您需要关注的另一件事是您正在右侧窗口上工作。
  • 我使用了元素的点击功能以及 aWindow.FindElementByAccessibilityId("Login").Click();但是这个语句(aWindow.FindElementByAccessibilityId("Login");) 本身正在引发异常。而且按钮是可点击的。

标签: c# appium-desktop winappdriver


【解决方案1】:

这个用户名密码字段是否显示在弹出窗口中?

启动应用程序后,请在尝试访问应用程序 UI 元素之前先进行短暂睡眠。我建议以下。

System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10));

更好的方法是使用 WebDriverWait 类的实例来等待元素被加载。

WebDriverWait wdv = new WebDriverWait(sessionAppWinForms, TimeSpan.FromSeconds(10));
var txtPwd = aWindow.FindElementByAccessibilityId("TxtPwd");
wdv.Until(x => txtPwd.Displayed);

更新: 我建议使用 WinAppDriver UI Recorder 检查 UI 控件。最新版本无法在我的 PC 上运行,因此我建议使用 1.0 版。下载链接如下。 https://github.com/microsoft/WinAppDriver/releases/tag/UiR_v1.0-RC

WinAppDriver 只是一个辅助程序,您无需使用它就可以创建自动化脚本。 有时应用程序需要更长的时间才能启动,在这种情况下,您可能会使用 WebDriverWait 类来等待某些条件成立。例如,等待某个标签或文本框出现在屏幕上。 您可以使用以下代码行无条件地等待几秒钟。

System.Threading.Thread.Sleep(5000);

我在 C# .Net 中使用 WinAppDriver 教授有关测试自动机的 Udemy 课程。详细介绍了这些概念。你可能会看到它here

【讨论】:

  • 我试着保持 Thread.Sleep(TimeSpan.FromSeconds(10));但它没有用。而且我还尝试了 aWindow.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);而不是 webdriverwait。没有运气。
  • Anil,这个用户名/密码字段是否显示在弹出框中?如果是,请尝试使用“app”、“Root”创建桌面会话。找到包含凭据字段的窗口,并在该窗口上执行操作。
  • No Naeem,那些用户名和密码是普通 WPF 页面上的两个控件。它实际上是一个具有这些控件的简单 WPF 应用程序。而且,我已经尝试过使用“app”、“Root”进行桌面会话,但没有成功。
  • 好的,WinAppDriver UI Recorder 显示了哪些控件属性?您是否尝试过 UI Recorder 使用“FindElementByXPath”方法生成的 XPath。
  • 我没有使用 UI Recorder。我只是使用 WinAppDriver 并使用检查工具获取控件的属性。
【解决方案2】:

该应用程序可能会在您的aWindow 范围之外的另一个窗口中打开。

您可以尝试创建一个desktop driver session 并使用Process.Start() 方法启动您的进程。

【讨论】:

    【解决方案3】:

    如果您的应用程序以管理员身份运行,则 WinAppDriver 和 Inspect.exe 也必须以管理员身份运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-28
      • 2019-12-07
      • 2020-08-11
      • 2018-02-10
      • 2020-04-12
      • 2019-07-26
      • 1970-01-01
      • 2019-09-30
      相关资源
      最近更新 更多