0

我是自动化测试的新手。我正在尝试使用 appium 在 Android 应用程序的 webview 中查找元素。下面是我正在使用的代码,但我无法使用它的 id找到说div

private AppiumDriver<WebElement> driver;

@Before
public void setUp() throws Exception {
    // set up appium


}
@After
public void tearDown() throws Exception {
    driver.quit();
}

@Test
public void webView() throws InterruptedException {

     DesiredCapabilities capabilities = new DesiredCapabilities();
     capabilities.setCapability("deviceName","Android Emulator");

     capabilities.setCapability("appPackage", "com.hokucampusaffairs");
     capabilities.setCapability("appActivity", "com.mybeeps.DashboardActivity");
     try {
        driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Thread.sleep(6000);
   Set<String> contextNames = driver.getContextHandles();
    for (String contextName : contextNames) {
      System.out.println(contextName);
      if (contextName.contains("WEBVIEW")){
        driver.context(contextName);
      }
    }
 String pagesource=   driver.getPageSource();
 String  url=   driver.getCurrentUrl();
 try {
    WebElement inputField = **driver.findElement(By.id("top-container"));**
    inputField.click();
 }
 catch(Exception e)
 {
     e.printStackTrace();
 }

}

粗体代码没有找到 div 使用它的 id 。findelementbyTag 也只是查找父元素。

我正在使用最新版本的 appium /Android sdk/java 1.8

4

1 回答 1

0

异常给你答案 - 没有找到元素id='top-container'

  1. 确保将驱动程序切换到 webview
  2. 尝试其他定位器策略,例如 Xpath
  3. 对于 webview 检查,您可以使用 chrome 开发工具:https ://developers.google.com/web/tools/chrome-devtools/remote-debugging/webviews
于 2017-12-09T08:52:45.397 回答