0

How to implement or condition in xpath to get text inside the table td ?

From the below table, my expected output should be:

Black bean sauce
Black bean chicken
USA(New York)
India(New Delhi)

 <table>
        <tr class="foods foods_s-fndds2-f41205100">                    
                <td class="name">                   
                    <a href="Javascript://">Black bean sauce</a>
                </td>                     
                <td class="calories">280</td> 
         </tr>
         <tr class="foods foods_s-fndds2-f41205140">                    
                <td class="name">                   
                    <a href="Javascript://">Black bean chicken</a>
                </td>                     
                <td class="calories">280</td> 
         </tr>
         <tr class="foods foods_s-fndds2-f41210200"> 
            <td class="name">  
                <span class="truncated"><a href="Javascript://">kathmandu…</a></span>
                <span class="complete"><a href="Javascript://">USA(New</a><br>
                York)</span>
            </td> 
       </tr>
        <tr class="folder foods foods_undefined">
            <td class="name" colspan="4"><span></span>Black, brown, or Bayo beans, dry, cooked</td>
            <td class="actions"><a class="open" href="Javascript://"></a></td>
       </tr>
       <tr class="foods foods_s-fndds2-f41210200"> 
            <td class="name">  
                <span class="truncated"><a href="Javascript://">kathmandu…</a></span>
                <span class="complete"><a href="Javascript://">India(New</a><br>
                Delhi)</span>
            </td> 
       </tr>
</table>

i tried this query but it gives only

Black bean sauce
Black bean chicken

code i tried:

   List<WebElement> folder = driver.findElements(By.xpath("//tr/td[@class='name']/a"));
        for (WebElement webElement : folder) { 
            String foodName = webElement.getText();
            System.out.println("text======" + foodName);
  }
Dan
  • 2,086
  • 11
  • 71
  • 137
  • I would rather use html parser like JSoup for this purpose – jmj Jun 10 '14 at 00:15
  • @JigarJoshi can you tell me how to do that.. please post your answer :) thanks – Dan Jun 10 '14 at 00:16
  • You can write something like [this](http://stackoverflow.com/questions/8222118/using-jsoup-to-extract-html-table-contents) to get table content and then you will have to find method to strip out html and just get the text data from tag – jmj Jun 10 '14 at 00:17
  • @JigarJoshi Thanks , can you please post answer for me also.. I am doing selenium since 1 week, do not have much confidence on doing advance stuffs now itself. It could very helpfull if you can help me by posting answer as i requesting in above question – Dan Jun 10 '14 at 00:19

1 Answers1

2

Use pipe | to select more nodes:

//tr/td[@class='name']/a | //tr/td[@class='name']/span[@class='complete']

See XPath or operator for different nodes

Community
  • 1
  • 1
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
  • Thanks but your answer is not complete , please check my expected output , this above code is not giving as i am expecting output. – Dan Jun 10 '14 at 00:47
  • can you tell me, how can i click on hyperlink text, i want to click on `kathmandu…` on my table tag – Dan Jun 10 '14 at 06:22
  • This can be shortened to: `//tr/td[@class='name']/(a | span[@class='complete'])` – adamretter Jun 10 '14 at 08:47