我希望有人可以幫助我,我堅持使用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=""> </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"> </td>
<td class="cell_sh part-bottom"> </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"> </td>
<td class="cell_tf part-top"> </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=""> </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"));
}
}
是的,但這不是因爲發現在一個聲明中的元素一樣有效,特別是如果你使用的是遠程網頁驅動 –