Reputation: 167
How can we extract ABC, XYZ by using xpath
<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/>
I tried with
response.xpath('//div[span="Address:"]/span/text()').extract()
but then I get [Name, ABC,XYZ, 20]
but i require only ABC, XYZ
.
Upvotes: 1
Views: 436
Reputation: 633
//div[@id="desc"]//span[text()='Address:']//following::span[1]/text()
Use this xpath instead.
or
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);
}
}
You can loop through the element lists.
Upvotes: 0
Reputation: 52665
Try to use below XPath
to get required output:
//div[@id="desc"]/span[.="Address:"]/following-sibling::span[1]/text()
Upvotes: 1