java - 使用正则表达式时appium驱动程序抛出异常?

标签 java selenium appium

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

最佳答案

您有两个问题:

  1. 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/

    相关文章:

    xcode - 如何通过 xcode 应用程序将当前目录路径(路径中的空格)发送到终端

    java - Appium 能够看到屏幕上显示之外的内容

    Java-寻找有关计算函数的最小值/最大值或步长间隔中的导数的建议

    java - 如何解决错误找不到 Retention 和 IntDef 的符号类?

    java - 如何确保我的 Android 应用程序不会同时访问文件?

    java - 尝试在 TestNG 中使用 ExtentReports 生成测试报告时获取 NullPointerException

    javascript - 在 selenium Anchor 标签中,onclick 函数不起作用?

    python - 问题: Getting to the element by using CSS selectors attribute 'style' with selenium

    java - 使用 where 子句加入多个实体并从中获取结果

    c# - 无法检测到 webview Appium