首页 > 解决方案 > Appium (Webdriver.io) 无法通过 xpath 找到元素

问题描述

我是 Appium 的新手,所以可能会做错事 =(

技术:TypeScript+Appium(wd.io)+Jasmine

但我一直坚持收到错误:

茉莉花原木:

An element could not be located on the page using the given search parameters ("//android.view.ViewGroup[@resource-id="com.app.dev.debug:id/toolbar"]//android.widget.TextView")

Appium日志:

[HTTP] --> POST /wd/hub/session/03ca8b58-7487-4ef6-9b81-8287a23c4a48/element {"using":"xpath","value":"//android.view.ViewGroup[@resource-id=\"com.app.dev.debug:id/toolbar\"]//android.widget.TextView"}
[debug] [MJSONWP] Calling AppiumDriver.findElement() with args: ["xpath","//android.view.ViewGroup[@resource-id=\"com.app.dev.debug:id/toolbar\"]//android.widget.TextView","03ca8b58-7487-4ef6-9b81-8287a23c4a48"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.view.ViewGroup[@resource-id=\"com.app.dev.debug:id/toolbar\"]//android.widget.TextView","context":"","multiple":false}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.view.ViewGroup[@resource-id=\"com.app.dev.debug:id/toolbar\"]//android.widget.TextView","context":"","multiple":false}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding '//android.view.ViewGroup[@resource-id="com.app.dev.debug:id/toolbar"]//android.widget.TextView' using 'XPATH' with the contextId: '' multiple: false
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding '//android.view.ViewGroup[@resource-id="com.app.dev.debug:id/toolbar"]//android.widget.TextView' using 'XPATH' with the contextId: '' multiple: false
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":7,"value":"No element found"}
[debug] [AndroidBootstrap] Received command result from bootstrap
[HTTP] <-- POST /wd/hub/session/03ca8b58-7487-4ef6-9b81-8287a23c4a48/element 500 232 ms - 164 

通过 xpath 查找元素时。

附上元素树: 元素树

下面的代码示例:

const deviceOptions: ServiceOptions = {
  desiredCapabilities: {
    platformName: 'android',
    app: './app-dev-debug.apk',
    appPackage: 'com.app.dev.debug',
    appActivity: 'com.app.dev.feature.start.StartActivity',
    avdReadyTimeout: 1000,
    udid: 'LGK350RGNBS4TS',
    deviceName: 'LG-K350',
    clearSystemFiles: true,
    newCommandTimeout: 120
  },
    host: 'localhost',
    port: 4723
  };
}

const driver = remote(deviceOptions);

const selector = '//android.view.ViewGroup[@resource-id="com.app.dev.debug:id/toolbar"]//android.widget.TextView';

it('test', () =>
  driver.init().element(selector).getText().then(text => expect(text).toBe('myText'))
);

这里有什么问题?如何让它发挥作用?

标签: androidautomated-testsappiumwebdriver-io

解决方案


找到了解决方案:添加了.waitForExist(selector)

it('test', () =>
  driver.init().waitForExist(selector).element(selector).getText().then(text => 
expect(text).toBe('myText'))
);

推荐阅读