Appium(Webdriver.io)无法通过xpath找到元素

时间:2018-05-08 13:45:09

标签: android automated-tests appium webdriver-io

我是Appium的新手所以可能做错了=(

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

但我一直坚持接收错误:

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查找元素时。

元素树附: elements tree

下面的代码示例:

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'))
);

这里有什么问题?如何使它工作?

2 个答案:

答案 0 :(得分:0)

在您的选择器中,您似乎没有提供应该使用哪个'By'来查找元素。

请尝试以下方式

import subprocess

value1=...
value2=...
......

val = subprocess.check_call(["path/script.sh",  value1+" "+value2+" "+ .... ],  shell=True) 

希望这有帮助

答案 1 :(得分:0)

找到解决方案: 添加了 .waitForExist(选择器)

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