java - Xpath 无法找到文本

标签 java xpath selenium-webdriver

HTML 代码

<div class="filterInfoContainer">
    <div class="result-header-right">
      <div class="show-soldout">
        <label for="soldout-switch">Display sold out products</label>
        <label class="switch xs">
          <input type="checkbox" name="soldout-switch" id="soldout-switch">
          <div class="switch-slider"></div>
        </label>
      </div>
      <div class="sort-by">Sorted by:
        <select id="sortByDropdown">
          <option value="popularity">Popularity</option>
          <option value="price">Lowest price</option>
          <option value="discount">Highest discount</option>
          <option value="year">Latest release</option>
        </select>
      </div>
    </div>
      Showing 323 products
    <div class="clearfix"></div>

  </div>

我试过的Xpath

.//*[@id='filterInfo']/div
.//*[@id='filterInfo']/div/text()

我想获取文本显示 323 种产品,但我的 xpath 没有返回我。由于此文本没有特定的 div,因此我很难获得它。

我试过的Selenium代码

 driver.findElement(By.xpath(".//*[@id='filterInfo']/div"));

 driver.findElement(By.xpath(".//*[@id='filterInfo']/div/text()"));

Getting exception : Invalid Selector

最佳答案

//div/text()可以得到Showing 323 products。 基本上它将返回未包含在标签中的文本。 请引用这个example .

所以在你的情况下:

//div[@class='filterInfoContainer']/text()

http://www.xpathtester.com/xpath/d06b22fb023db960d8fc619485759127

关于java - Xpath 无法找到文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42974532/

相关文章:

java - 如何停止 Netbeans 中正在运行的进程

java - Spark Streaming,如何将不同的流合并为一个流

xml - 计算 XML 文档中特定节点的出现频率

c# - Selenium 2 WebDriver - Chrome - 从通过 JavaScript 设置的文本框中获取值

c# - ChromeDriver 显示丢失的 UI 共享上下文

java - 获取 HTTP 状态 500 - org.hibernate.internal.util.config.ConfigurationException : Specified cfg. xml 文件不存在

由 xpath : search by text in child elements 提供的 Selenium WebDriver

python - 如何在selenium中获取当前选项卡的url?

java - FluentWait 忽略不按预期工作

java - 获取 MismatchedInputException : Cannot deserialize instance of Object while parsing JSON to result object