我正在使用 appium 进行一些 ui 自动化。我在正则表达式方面遇到问题;
WebDriverException 与代码一起抛出,并且该元素完全存在:
phonedriver.findElement(By.xpath("//android.view.View[matches(@text,'sometext\\d+')]"))
以下是异常消息:
An unknown server-side error occurred while processing the command. Original error: java.lang.IllegalStateException: Unable to evaluate expression. See cause
这是我的能力信息:
Capabilities {appActivity: com.tencent.mm.ui.LauncherUI, appPackage: com.tencent.mm, deviceName: 127.0.0.1:62001, fastReset: false, fullReset: false, newCommandTimeout: 999999, noReset: true, platformName: Android, platformVersion: 5.1.1, resetKeyboard: true, udid: 127.0.0.1:62001}
这是我的 pom:
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.3.0</version>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
最佳答案
您有两个问题:
matches()
仅在 XPath 2.0 中可用,在 1.0 中不可用,请参阅:http://www.w3.org/TR/xquery-operators/#func-matches
Selenium、Chrome 和 Firefox 浏览器都在使用 Xpath 1.0。
您可以在浏览器的 JS 控制台中尝试任何 Xpath 2.0 功能,它会显示错误。例如: $x("lower-case('ABC')")
- 要获取文本,请使用
.
或text()
例如$x("//*[contains(text(),'bob')] ”)
作为问题的解决方案,您可以获取包含文本的所有元素 "//android.view.View[contans(text(),'sometext')]")
然后循环在JAVA中通过它们找到正确的
关于java - 使用正则表达式时appium驱动程序抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60825398/