2014-01-21 47 views
1

我希望有人可以帮助我,我坚持使用XPath表达式。我有以下的HTML从AA tennismatch提取与C#和硒:XPath查询检查td是否有某些孩子(C#+ Selenium)

<tbody> 
    <tr id="g_2_CvUnLIxC" class="tr-first stage-live" style="cursor: pointer;" title=""> 
    <td rowspan="2" class="cell_ib icons left"><span class="icons left"><span class="tomyg icon0"></span></span></td> 
    <td rowspan="2" class="cell_ad time time-playing" title="Click for match detail!">21:00</td> 
    <td rowspan="2" class="cell_aa timer playing" title="Click for match detail!"><span>Set 3</span></td> 
    <td class="cell_xh serve-home" title="">&nbsp;</td> 
    <td class="cell_ab team-home" title="Click for match detail!"><span class="padl">Barrientos N. (Col)</span></td> 
    <td class="cell_sc score-home bold playing">1</td> 
    <td class="cell_sd part-bottom" title="Click for match detail!">7</td> 
    <td class="cell_se part-bottom" title="Click for match detail!">2</td> 
    <td class="cell_sf part-bottom">4</td> 
    <td class="cell_sg part-bottom">&nbsp;</td> 
    <td class="cell_sh part-bottom">&nbsp;</td> 
    <td class="cell_sp part-bottom highlight-highlighted">15</td> 
    <td rowspan="2" class="cell_ia icons"><span class="icons"><span class="tv icon1"></span></span></td> 
    <td rowspan="2" class="cell_oq comparison"><span class="icons"><span class="clive icon0"></span></span></td> 
    </tr> 
    <tr id="x_2_CvUnLIxC" class="tr-first stage-live" style="cursor: pointer;" title=""> 
    <td class="cell_xa serve-away" title=""><span class="icons" title=""><span class="tennis-serve" title="Serving player"></span></span></td> 
    <td class="cell_ac team-away" title="Click for match detail!"><span class="padl">Lapentti G. (Ecu)</span></td> 
    <td class="cell_ta score-away bold playing" title="Click for match detail!">1</td> 
    <td class="cell_tb part-top">5</td> 
    <td class="cell_tc part-top">6</td> 
    <td class="cell_td part-top">5</td> 
    <td class="cell_te part-top">&nbsp;</td> 
    <td class="cell_tf part-top">&nbsp;</td> 
    <td class="cell_to part-top highlight-highlighted" title="Click for match detail!">30</td> 
    </tr> 
</tbody> 

出于这个我想提取该玩家当前正在服役,这是由所显示具有以下跨度标签球符号显示一个TD-标签中:

<span class="icons" title=""><span class="tennis-serve" title="Serving player"></span></span> 

这要么设置在服务家庭或服务外卖:

<td class="cell_xh serve-home" title="">&nbsp;</td> 
<td class="cell_xa serve-away" title=""><span class="icons" title=""><span class="tennis-serve" title="Serving player"></span></span></td> 

我要的是类似如下的伪代码类似于S:

IWebElement id = driver.FindElement(union ((By.Id(id1),By.Id(id2)); 
IWebElement serve = id.FindElement(By.XPath("td that contains <span class="icons" title=""><span class="tennis-serve" title="Serving player"></span></span> 

String serv 

er = serve.getAttribut("class"); //contains then serve-home or serve-away 

Find attached an example of what the website looks like, I want to find out if the ball is displayed at player 1 or 2: 
[link](http://i44.tinypic.com/2e157v6.png) 

Thank you in advance! 

UPDATE1:

   IWebElement id1 = driver.FindElement(By.Id(txt_ID1.Text)); 
       IWebElement id2 = driver.FindElement(By.Id(txt_ID2.Text)); 

       ReadOnlyCollection<OpenQA.Selenium.IWebElement> elements = id1.FindElements(By.XPath("td[contains(@class,'serve')]")); 
       ReadOnlyCollection<OpenQA.Selenium.IWebElement> elements2 = id2.FindElements(By.XPath("td[contains(@class,'serve')]")); 

       foreach (IWebElement i in elements) 
       { 
        if (i.GetAttribute("innerHTML").Contains("span")) 
         Console.WriteLine(i.GetAttribute("class")); 
       } 

       foreach (IWebElement i in elements2) 
       { 
        if (i.GetAttribute("innerHTML").Contains("span")) 
         Console.WriteLine(i.GetAttribute("class")); 
       } 

}

回答

0

不知道关于C#的,但这里是我会怎么做它在Java中:

首先,找到所有的<td>其中class属性包含的元素"serve"

List<WebElement> elements = driver.findElements(By.xpath("//td[contains(@class,'serve')]")); 

然后,找到第一<td>元素,其innerHTML属性包含"span"

for (WebElement element : elements) 
    if (element.getAttribute("innerHTML").contains("span")) 
     return element.getAttribute("class"); 
+0

是的,但这不是因为发现在一个声明中的元素一样有效,特别是如果你使用的是远程网页驱动 –

1

您可以使用XPath序列..得到一个确定的元素的父。既然你正在寻找的td<span class="tennis-serve" >的直接母公司,它可以用来像这样:

//assuming this line correctly locates the table we are interested in 
IWebElement id = driver.FindElement(union ((By.Id(id1),By.Id(id2)); 

//select all the td elements containing the span with class="tennis-serve" in the above table 
//the starting '.' looks for descendants of only the context node 'id' 
//if you want all serving players, use FindElements 
var servers = id.FindElements(By.XPath(".//span[@class='tennis-serve']/..")); 

foreach(var s in servers) 
{ 
    string servingBy = s.GetAttribute("class"); 
    Console.WriteLine(servingBy); 
} 
+0

完美,非常感谢! – jinx

+0

对不起,更改了“正确”的答案,它使用萤火虫进行了快速测试,但在我的实际代码中,它总是返回表中第一个匹配的服务,而不是选定的。我得到了@barak manos的解决方案来处理一个小的变化(删除尾部// asthis似乎使查询返回主dom而不是已选择的子集) - 请参阅UPDATE1。不过,我仍然对学习如何使用/ ..运算符感兴趣。 – jinx

+0

已经更新了xpath以查找只有上下文节点的后代,这将是'id'。确保它的定位器选择了正确的表格。 – Faiz