如何使用xpath
提取ABC,XYZ<div id="desc" class="description">
<span class="category">Name:</span> <span class="category-detail"><a href="/name/">Name</a></span>
<br/>
<span class="category">Address:</span> <span class="category-detail">ABC, XYZ</span>
<br/>
<span class="category">Room No:</span> <span class="category-detail">20</span>
<br/>
我试过
response.xpath('//div[span="Address:"]/span/text()').extract()
然后我得到[Name, ABC,XYZ, 20]
,但我只需要ABC, XYZ
。
答案 0 :(得分:1)
尝试在XPath
下面使用以获得所需的输出:
//div[@id="desc"]/span[.="Address:"]/following-sibling::span[1]/text()
答案 1 :(得分:0)
//div[@id="desc"]//span[text()='Address:']//following::span[1]/text()
请改用此xpath。
或
WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/sv/Desktop/docUpload.html");
List<WebElement> spanList = driver.findElements(By.xpath(".//div[@id='desc']/span"));
for (int i =0 ;i < spanList.size();i++){
String text = spanList.get(i).getText();
if(text.equals("ABC, XYZ")){
System.out.println(text);
}
}
您可以遍历元素列表。