【问题标题】:How to use apostrophe (') in xpath while finding element using webdriver?使用webdriver查找元素时如何在xpath中使用撇号(')?
【发布时间】:2016-09-29 07:02:30
【问题描述】:

我需要在我的 xpath 表达式中使用撇号 ('),我需要在使用 webdriver 查找元素时使用它

我需要使用下面的Xpath表达式

//input[@text="WE'd like to hear from you"]

在查找元素函数中使用上述表达式时,我将双引号替换为单引号

driver.findelements(By.xpath("//input[@text='WE'd like to hear from you']"))

【问题讨论】:

    标签: java selenium xpath


    【解决方案1】:

    使用如下所示的xpath:

    driver.findElements(By.xpath("//input[contains(@text,\"WE'd\")]"));
    

    希望这会有所帮助。

    【讨论】:

    • 在我的例子中,我得到的文本是动态的,可以带单引号或不带单引号。我们将如何动态处理
    【解决方案2】:

    您必须使用双引号作为 XPath 字符串文字分隔符,因为 XPath 1.0 不提供转义引号的方法。除此之外,您可以在 Java 中转义双引号,以避免它与您的 Java 字符串分隔符冲突,后者也使用双引号:

    driver.findelements(By.xpath("//input[@text=\"WE'd like to hear from you\"]"))
    

    【讨论】:

      【解决方案3】:

      转义字符的使用没有达到目的。我尝试了连接功能,它就像一个魅力。请参阅下面的 xpath。

      tag: li Manager of Workflow Initiator's Manager /li

      连接函数并将字符串拆分为-

      concat('Manager of Workflow Initiator',"'",'s Manager')
      

      单引号保留在双引号中,而其他字符保留在单引号中..

      所以 XPath 看起来像 -

      //li[.=concat('Manager of Workflow Initiator',"'",'s Manager')]
      

      【讨论】:

      • 如果上述解决方案不起作用,则使用转义序列使用以下解决方案。 xpath: //li[.=\"Manager of Workflow Initiator's Manager\"] 这里我们把整个文本当作一个字符串来处理。
      【解决方案4】:

      如果上述解决方案不起作用,则使用转义序列使用以下解决方案。

      xpath: //li[.=\"Manager of Workflow Initiator's Manager\"]
      

      这里我们使用转义字符将整个文本视为字符串

      【讨论】:

        【解决方案5】:

        我遇到过类似的情况,需要为如下所示的元素编写 xpath:

        元素:

        <img src="webwb/pzspacer.gif!!.gif" class="inactvIcon" data-ctl="["DatePicker"]" style="cursor:pointer;">
        

        我能够使用下面的 Xpath 对元素进行 grep,其中我使用反斜杠来转义字符 [ 和 "。

        Xpath//img[@data-ctl='\[\"DatePicker\"\]']

        希望这会有所帮助。

        【讨论】:

          【解决方案6】:

          上述方法都没有涵盖同时存在 Quote 和 Apostrophe cos 的情况。我为此创建了一个函数,

          driver.findElements(By.xpath(String.format("//input[contains(@text,%s))]"),escapeQuotes(textVal));
          

          转义引用的实现。

          private String escapeQuotes(String text) {
          
              // If we don't have any Quote then enquote string in Quote
              if (!text.contains("\"")) {
                  return String.format("\"%s\"", text);
              }
          
              // If we have some Quote but no Apostrophe then enquote in Apostrophe
              if (!text.contains("'")) {
                  return String.format("'%s'", text);
              }
          
              // If input is like Administr"ati'on then we have both " and ' in the string so must use Concat
              // we will be building the xPath like below and let the browser xPath evaluation to handle the concatenation
              // output : concat('Administr\"',\"ati'on\")
              StringBuilder sb = new StringBuilder("concat(");
          
              // Looking for " as they are LESS likely than '
              int lastPos = 0;
              int nextPos = text.indexOf("\"");
              while (nextPos != -1) {
                  // If this is not the first time through the loop then seperate arguments with ,
                  if (lastPos != 0) {
                      sb.append(",");
                  }
          
                  sb.append(String.format("\"%s\",'\"'", text.substring(lastPos, nextPos - lastPos)));
                  lastPos = ++nextPos;
          
                  // Find next occurrence
                  nextPos = text.indexOf("\"", lastPos);
              }
          
              sb.append(String.format(",\"%s\")", text.substring(lastPos)));
              return sb.toString();
          }
          

          【讨论】:

            猜你喜欢
            • 2023-03-18
            • 1970-01-01
            • 2011-04-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-03-31
            • 1970-01-01
            相关资源
            最近更新 更多