简体   繁体   English

Appium xpath 转义撇号

[英]Appium xpath escaping apostrophes

I have the code below in my appium script:我的 appium 脚本中有以下代码:

    public boolean isErrorDisplayedUnrecognisedLoginCredentials() {
        return appDriver.isElementExist(By.xpath("//UIAStaticText[@name='We don't recognize this user ID or password']"));
    }

The test is hanging since it treats the apostrophe in "don't" as a final apostrophe to close off the @name value.测试挂起,因为它将“don't”中的撇号视为关闭@name 值的最终撇号。 I have tried escaping the apostrophe in "don't" by using \\', \\', '我曾尝试使用 \\', \\', ' 来转义“不要”中的撇号

However, none of these are working and the tests keep failing.但是,这些都不起作用,并且测试不断失败。 Anyone know how to get round this?有谁知道如何解决这个问题?

AFAIK, you can't escape quotes in xpath, but you can escape quotes in Java. AFAIK,你不能在 xpath 中转义引号,但你可以在 Java 中转义引号。 So try to use escaped double quotes for the xpath literal string delimiter, for example :因此,尝试对 xpath 文字字符串分隔符使用转义双引号,例如:

By.xpath("//UIAStaticText[@name=\"We don't recognize this user ID or password\"]")

With XPath 1.0 (It should work with XPath 2.0):使用 XPath 1.0(它应该适用于 XPath 2.0):

By.xpath("//UIAStaticText[@name='We don't recognize this user ID or password']")

With XPath 2.0 we have two solutions:对于 XPath 2.0,我们有两种解决方案:

  1. By doubling the single apostrophe you will be able to escape the apostrophe: By.xpath("//UIAStaticText[@name='We don''t recognize this user ID or password']")通过将单个撇号加倍,您将能够转义撇号: By.xpath("//UIAStaticText[@name='We don''t recognize this user ID or password']")

Recommended solution:推荐解决方案:

  1. To define the Xpath, I would use the double quote (") instead of a apostrophe: By.xpath("//UIAStaticText[@name="We don't recognize this user ID or password"]")要定义 Xpath,我将使用双引号 (") 而不是撇号: By.xpath("//UIAStaticText[@name="We don't recognize this user ID or password"]")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM