Dan
Dan

Reputation: 2174

or condition in selenium xpath

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);
  }

Upvotes: 0

Views: 3775

Answers (1)

Yi Zeng
Yi Zeng

Reputation: 32855

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

Upvotes: 2

Related Questions