我正在使用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/