我想在使用webdriver时从网站获取一些分数。到目前为止,我尝试了XPath,CSS,Classname,但有时它位于该项目中,有时却不是。Selenium WebDriver - 无法找到元素异常
这是我一直在试图把它的HTML代码:
<td class="score" rowspan="6"><span class="p1_home">0</span> - <span class="p1_away">0</span></td>
这是我的代码(我试过至今):
firstHalf[i] = Driver.FindElement(By.XPath("//*[@id=\"parts\"]/tbody/tr[2]/td[2]")).Text;
其他版本:
firstHalf[i] = Driver.FindElement(By.CssSelector("#parts > tbody > tr:nth-child(2) > td.score")).Text;
firstHalf[i] = Driver.FindElement(By.ClassName("score")).Text;
而且我尝试了儿童班(在分数班),但结果是一样的,有时可以找到元素,有时它不能。
有什么建议吗?
更新:对于我的代码,我把一些wait
或Thread.Sleep
仍然无法正常工作。
for (int r = 0; r < 10; r++)
{
try
{
string d1 = Driver.FindElement(By.XPath("//span[@class='p1_home']")).Text;
string d2 = Driver.FindElement(By.XPath("//span[@class='p1_away']")).Text;
//firstHalf[i] = firstHalf[i].Replace(" ", "");
firstHalf[i] = d1 + "-" + d2;
break;
}
catch (Exception e)
{
Thread.Sleep(300);
r -= 1;
eventCounter++;
if (eventCounter == 10)
{
errorMsg.sendErrMsg(e);
//errorMsg.stopProgram();
}
}
}
ü要哪个元素定位 – iamsankalp89
@ iamsankalp89我想找到类'score'或子类,这些都是'p1_home'和'p1_away' –
//跨度[@ class ='p1_home']和// span [@ class ='p1_away'] – iamsankalp89