或在硒xpath中的条件

时间:2014-06-10 00:12:15

标签: java selenium xpath

如何在xpath中实现或调整条件td中的文字?

从下表中,我的预期输出应为:

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>

我尝试了这个查询,但只提供了

Black bean sauce
Black bean chicken

我试过的代码:

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

1 个答案:

答案 0 :(得分:2)

使用管道|选择更多节点:

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

请参阅XPath or operator for different nodes