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/