android - 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'))
);
这里有什么问题?如何让它发挥作用?
解决方案
找到了解决方案:添加了.waitForExist(selector)
it('test', () =>
driver.init().waitForExist(selector).element(selector).getText().then(text =>
expect(text).toBe('myText'))
);
推荐阅读
- regex - 如何编写具有子字符串匹配的正则表达式并排除另一个子字符串
- r - 使用两个嵌套的 foreach 并行循环,其中 foreach 是函数的一部分
- c# - 如何在虚拟文件夹中运行 ASP.NET MVC Core Web 应用程序
- python - Django 在 html 文件中使用视图响应
- linux - 如果满足条件,如何在重启后自动启动 Linux 服务?
- python - 为什么无法获得 title 属性?
- google-analytics - 如何将内容数据导入 Google Analytics?
- r - 在 dplyr 1.0.0 中运行缓慢的分组 tibble 中的 n_distinct
- python - 熊猫日期时间 index.is_quarter_end 错误
- awk - awk,为什么在代码中输入时不读取零?