【问题标题】:Why am i getting a Cannot implicitly convert type error for AppiumWebElement on a WindowsElement为什么我在 WindowsElement 上收到无法隐式转换 AppiumWebElement 的类型错误
【发布时间】:2020-08-24 02:48:24
【问题描述】:

我正在使用 WinAppDriver 学习 Appium。我正在尝试连接到已经在桌面上启动的应用程序,例如'记事本',然后单击最大化按钮。

然而,这段代码

WindowsElement maximizeButton = notepad.FindElementByName("Maximize");

给我这个错误:

Cannot implicitly convert type 'OpenQA.Selenium.Appium.AppiumWebElement' to 
'OpenQA.Selenium.Appium.Windows.WindowsElement'. An explicit conversion exists (are you missing a cast?)

我不知道为什么会发生这种情况,因为 nodepad 被声明为 WindowsElement,而 MaximizeButton 是 WindowsElement。如果我将其声明为 var,我不会收到此错误。

但是为什么 WindowsElement 不起作用?

    [TestMethod]
    public void AttachToAnExistingAppWindow()
    {
        // https://github.com/Microsoft/WinAppDriver/wiki/Frequently-Asked-Questions/a8c02cfac47b4bf0c12c571b6010c403dcfe5e7f#when-and-how-to-attach-to-an-existing-app-window
        DesiredCapabilities appCapabilities = new DesiredCapabilities();
        appCapabilities.SetCapability("app", "Root");
        WindowsDriver<WindowsElement> DesktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);

        Assert.IsNotNull(DesktopSession);

        WindowsElement notepad = DesktopSession.FindElementByName("Untitled - Notepad");
        notepad.Click();

        WindowsElement maximizeButton = notepad.FindElementByName("Maximize");
        if (!maximizeButton.Text.Contains("Maximize"))
        {
             maximizeButton.Click();
        }
    }

【问题讨论】:

  • FindElementByName 被声明为返回一个AppiumWebElement。为什么您期望将该结果分配给WindowsElement 变量?它可能返回不是WindowsElement的东西(至少就编译器而言)。
  • 因此,如果我跳转到 DesktopSession.FindElementByName 的声明,它是“public W FindElementByName(string name);”,但如果我对 notepad.FindElementByName("Maximize"); 执行相同的操作;它的“公共 AppiumWebElement FindElementByName(字符串名称);”。即使我将记事本创建为 WindowsElement。如何解决这个问题或在记事本应用程序上找到最大化按钮?
  • 如果您确信结果 WindowElement(并且您需要它,以便访问特定于 WindowElement 的成员),只需转换为它。

标签: c# appium winappdriver


【解决方案1】:

你可以试试 WindowsElement 最大化按钮 = (WindowsElement)notepad.FindElementByName("最大化");

【讨论】:

    【解决方案2】:

    谢谢,我发现这个工作..

    WindowsDriver<WindowsElement> session;
    
    session.FindElementByName("Maximize").Click();
    session.FindElementByName("Restore").Click();
    
    // Or using Maximize and restore via xpath
    session.FindElementByXPath($"//Button[starts-with(@Name, \"Maximize\")]").Click();
    session.FindElementByXPath($"//Button[starts-with(@Name, \"Restore\")]").Click();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-04
      • 2011-06-13
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      相关资源
      最近更新 更多