2014-01-21 68 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