Java 如何使用AND运算符编写XPath以在单个XPath中添加多个跨距?

Java 如何使用AND运算符编写XPath以在单个XPath中添加多个跨距?,java,selenium,selenium-webdriver,xpath,webdriver,Java,Selenium,Selenium Webdriver,Xpath,Webdriver,该页面包含产品名称-(3个,共3个)GOLDEN GLOW(豪华)。 产品名称有6个不同的跨距,因此我们希望打印产品名称“GOLDEN GLOW(DELUXE)”,即包括所有跨距,因此我尝试在[]内多次使用和,但没有成功。下面是XPath: //*[@class='itemTitleCopy no-mobile' and contains(@class, 'no-mobile') and contains(@class, 'sizeDescriptionTitle no-mobile') con

该页面包含产品名称-(3个,共3个)GOLDEN GLOW(豪华)。 产品名称有6个不同的跨距,因此我们希望打印产品名称“GOLDEN GLOW(DELUXE)”,即包括所有跨距,因此我尝试在[]内多次使用和,但没有成功。下面是XPath:

//*[@class='itemTitleCopy no-mobile' and contains(@class, 'no-mobile') and contains(@class, 'sizeDescriptionTitle no-mobile') contains(@class, 'no-mobile') ]
以下是HTML代码:

<span class="m-shopping-cart-item-header-number">
   ( 
   <span id="itemNo-1" class="itemNo">3</span>
   of 
   <span id="totalItems-1" class="totalItems">3</span>
   )
   <span class="itemTitleCopy no-mobile" id="itemTitleCopy-1">Golden Glow</span>
   <span class="no-mobile">(</span>
   <span class="sizeDescriptionTitle no-mobile" id="sizeDescriptionTitle-1">Deluxe</span>
   <span class="no-mobile">)</span>
</span>
使用以下命令:

//*[@class=('itemTitleCopy no-mobile','sizeDescriptionTitle no-mobile','no-mobile')]
希望它能解决。

使用以下方法:

//*[@class=('itemTitleCopy no-mobile','sizeDescriptionTitle no-mobile','no-mobile')]

希望它能解决。

要提取文本Golden Glow(豪华版)您可以使用以下选项:

  • 使用XPath:

    String myString=driver.findElement(By.xpath(//span[@m-shopping-cart-item-header-number'])。getText();
    
    String[]parts=myString.split(“?要提取文本Golden Glow(豪华版),可以使用以下命令:

    • 使用XPath:

      String myString=driver.findElement(By.xpath(//span[@m-shopping-cart-item-header-number'])。getText();
      String[]parts=myString.split(“?
      应使用
      class=“totalItems”
      选择
      span
      之后的所有
      span
      节点。提取所需文本内容的方法可能不同,具体取决于Selenium绑定

      这是获取所需输出的Python代码:

      text = " ".join([span.text for span in driver.find_elements_by_xpath('//span[@class="totalItems"]/following-sibling::span')])
      print(text)
      #  'Golden Glow(Deluxe)'
      
      应使用
      class=“totalItems”
      选择
      span
      之后的所有
      span
      节点。提取所需文本内容的方法可能不同,具体取决于Selenium绑定

      这是获取所需输出的Python代码:

      text = " ".join([span.text for span in driver.find_elements_by_xpath('//span[@class="totalItems"]/following-sibling::span')])
      print(text)
      #  'Golden Glow(Deluxe)'
      

      您似乎对
      的含义感到困惑。谓词中的
      运算符意味着这两个条件都必须为真:它的限制性更强,因此通常选择的数据更少。
      运算符意味着这两个条件都必须为真:它更自由,因此需要更多的数据被选中


      你似乎认为“和”是“联合”的意思——选择X和(也选择)Y.这在布尔逻辑中从来就不是它的意义。

      您似乎对
      的含义感到困惑。谓词中的
      运算符意味着这两个条件都必须为真:它的限制性更强,因此通常选择的数据更少。
      运算符意味着这两个条件都必须为真没错:它更自由,所以会选择更多的数据


      你似乎认为“and”是“union”的意思——选择X和(也选择)Y。这在布尔逻辑中从来就不是它的意思。

      因为@Michael Kay已经回答了你需要的是使用
      操作符

      您可以使用
      findElements
      Selenium来实现这一点

      它应该是这样的:

      driver.findElements(By.xpath("//*[@class='itemTitleCopy no-mobile' or contains(@class, 'no-mobile') or contains(@class, 'sizeDescriptionTitle no-mobile')]"))
      
      这将返回
      WebElements的列表
      ,现在您可以迭代这些元素并加入文本以创建所需的
      “GOLDEN GLOW(豪华)”字符串


      所有的功劳都归功于@Michael Kay我刚才给了你一个例子…

      因为@Michael Kay已经回答了你需要的是使用
      操作员

      您可以使用
      findElements
      Selenium来实现这一点

      它应该是这样的:

      driver.findElements(By.xpath("//*[@class='itemTitleCopy no-mobile' or contains(@class, 'no-mobile') or contains(@class, 'sizeDescriptionTitle no-mobile')]"))
      
      这将返回
      WebElements的列表
      ,现在您可以迭代这些元素并加入文本以创建所需的
      “GOLDEN GLOW(豪华)”字符串


      所有的功劳都归功于@Michael Kay我刚才给了你一个例子…

      用HTML示例编辑你的问题,你当前的XPath,当前的输出,期望的输出我认为你在上一次会议之前缺少和运算符。发布XML将帮助我们找到问题。我想优先考虑产品名称,即Golden Glow(豪华版),忽略(3/3)因此,为了实现这一点,我尝试使用AND运算符编写Xptah//span[@class='itemtilecopy no-mobile',contains(@class',no-mobile')、contains(@class',sizeDescriptionTitle no-mobile')、contains(@class',no-mobile'),但它不起作用,我只能使用AND运算符一次,即//span[@class='itemtilecopy no mobile'并包含(@class='no mobile')]在此之前,我能够识别Elment,但如果我使用all the and运算符3次,我无法识别Elment。请使用HTML示例、当前XPath、当前输出、所需输出编辑您的问题。我认为您在最后一次会议之前缺少and运算符。发布XML,这将帮助我们找到问题。我想知道产品名称i、 e Golden Glow(豪华版)忽略(3/3),因此为了得到这个结果,我尝试使用AND运算符编写Xptah//span[@class='itemTitleCopy no mobile'和contains(@class',no mobile')、contains(@class',sizeDescriptionTitle no mobile')和contains(@class',no mobile')]它不起作用,我只能使用和操作一次,即//span[@class='itemtilecopy no mobile'和contains(@class='no mobile')]在此之前,我能够识别Elment,但是如果我使用了3次all the and运算符,我就无法识别Elment.org.openqa.selenium.InvalidSelectorException:无效选择器:无法找到xpath表达式为//*[@class=('itemtilecopy no-mobile','sizeDescriptionTitle no-mobile','no-mobile')的元素由于以下错误:SyntaxError:未能对“文档”执行“评估”:字符串“/*[@class=('itemTitleCopy no-mobile','sizeDescriptionTitle no-mobile','no-mobile')]“”不是有效的XPath表达式。这是Seleniumorg.openqa.selenium.InvalidSelectorException不支持的XPath 2.0语法:无效选择器:找不到XPath表达式为/*[@class=('itemTitleCopy no-mobile'、'sizeDescriptionTitle no-mobile'、'no-mobile')的元素由于以下错误:SyntaxError:未能对“文档”执行“评估”:字符串“/*[@class=('itemTitleCopy no-mobile'、'sizeDescriptionTitle no-mobile'、'no-mobile')]”不是有效的XPath表达式。这是SeleniumTried不支持的XPath 2.0语法