1

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.

Andersson
  • 51,635
  • 17
  • 77
  • 129
Pradeep Mishra
  • 137
  • 2
  • 12

2 Answers2

1

Try to use below XPath to get required output:

//div[@id="desc"]/span[.="Address:"]/following-sibling::span[1]/text()
Andersson
  • 51,635
  • 17
  • 77
  • 129
0
//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.

Sudha Velan
  • 633
  • 8
  • 24
  • Yes, Xpath gives proper result. Thanks.. btw could you please tel me know where i can learn these types of complex xpaths – Pradeep Mishra Aug 21 '17 at 09:46
  • https://www.w3schools.com/xml/xpath_axes.asp https://www.guru99.com/xpath-selenium.html http://toolsqa.com/selenium-webdriver/choosing-effective-xpath/ http://www.seleniumeasy.com/selenium-tutorials/xpath-tutorial-for-selenium , Go through these sites. – Sudha Velan Aug 21 '17 at 13:35
  • Thank you vey much – Pradeep Mishra Aug 22 '17 at 05:25