使用 FindElementByAccessibilityId 时出现 NoSuchElementException

NoSuchElementException when using FindElementByAccessibilityId

我是 Appium 的新手,但对 Selenium 很有经验。

第一步,点击元素 'Nieuw' 没问题,出现一个有四个选项的屏幕。

之后,我尝试单击具有辅助功能 ID 'Proefitmanager' 的元素。在 Appium 桌面中,这没有问题,但在我的 Appium 测试中,我遇到了 NoSuchElement 异常。我正在使用 Appium 桌面建议的 Id,我的代码与 Appium 桌面记录器生成的代码相当,尽管我使用的是 C# 而不是 Java。

IWebElement nieuw = (IWebElement)driver.FindElementByXPath("(//android.view.View[@content-desc='Nieuw'])[2]");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementToBeClickable(nieuw));
nieuw.Click();

IWebElement proefrit = (IWebElement)driver.FindElementByAccessibilityId("Proefrit");
wait.Until(ExpectedConditions.ElementToBeClickable(proefrit));
proefrit.Click();

我的想法是元素在点击的那一刻没有焦点,因为它在另一个框架左右。我尝试使用 SwitchTo().Frame(0) 和 Frame(1) 但这有例外情况,例如:

Could not proxy command to remote server. Original error: 404 - undefined`

查看来自 Appium 桌面的图像,了解应用程序的外观。

  1. 首先,您需要在单击 "Proefit" 元素之前从本机视图切换到 Web 视图。

下面是Java中的示例代码:

   Set<String> contextNames = driver.getContextHandles(); 
   for (String contextName : contextNames) {
     System.out.println(contextName); //prints out something like NATIVE_APP or WEBVIEW_1 
    } 
   driver.context(contextNames.toArray()[1]); // set context to WEBVIEW_1
  1. 然后对proefit元素进行点击操作

下面是Java中的示例代码:

WebElement proefrit=driver.findElementByAccessibilityId("Proefrit");
wait.Until(ExpectedConditions.ElementToBeClickable(proefrit));
proefrit.click();

3.Then 切换回 Native Context 继续测试。

driver.context("NATIVE_APP");