简体   繁体   English

Selenium WebDriver-无法找到元素异常

[英]Selenium WebDriver - Unable to locate element exception

I am trying to take some scores from websites while using webdriver. 我正在尝试使用webdriver从网站上获得一些分数。 I tried so far XPath, CSS, Classname but, sometimes it is located the item, sometimes it does not. 到目前为止,我尝试过XPath,CSS,Classname,但是有时它位于该项目中,有时却不在。

This is the HTML code that I have been trying to take it: 这是我一直在尝试的HTML代码:

 <td class="score" rowspan="6"><span class="p1_home">0</span> - <span class="p1_away">0</span></td> 

And this is my code (that i tried so far) : 这是我的代码(到目前为止我尝试过):

firstHalf[i] = Driver.FindElement(By.XPath("//*[@id=\"parts\"]/tbody/tr[2]/td[2]")).Text;

Other versions : 其他版本:

 firstHalf[i] = Driver.FindElement(By.CssSelector("#parts > tbody > tr:nth-child(2) > td.score")).Text;
 firstHalf[i] = Driver.FindElement(By.ClassName("score")).Text;

And also I tried the child classes (under score classes), but the result is same, sometimes can be located the element, sometimes it cannot. 我也尝试了子类(在分数类下),但结果是相同的,有时可以找到元素,有时则不能。

Any suggestion? 有什么建议吗?

Update : To my code, I put some wait or Thread.Sleep still will not work. 更新:对我的代码,我waitThread.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();
                        }
                    }
                }

You can use 您可以使用

//span[@class='p1_home']

//span[@class='p1_away']

To locate element of particular class and attribute you can use CSS selector. 要查找特定类和属性的元素,可以使用CSS选择器。 For example: 例如:

Driver.FindElement(By.CssSelector(".score[rowspan='6']")).Text;

In case you having trouble to locate element reliably, you might want get it after certain conditions are met, like so: 如果您无法可靠地定位元素,则可能需要在满足某些条件后才能获取它,例如:

WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector(".score[rowspan='6']")));

Okay, I found a solution with a different approach. 好的,我找到了另一种方法的解决方案。 Instead of getting two specific class, I used parent class score . 我没有获得两个特定的班级,而是使用了父班级score Also, I deleted the "for loop" and "try" and "catch" which I am using for finding the object again and again. 另外,我删除了用于反复查找对象的“ for循环”“ try”“ catch”

To my main loop, I added "try" and "catch" . 在我的主循环中,添加了“ try”“ catch” When I got the exception, I decreased the loop counter, closed the window and opened again. 收到异常后,我减少了循环计数器,关闭了窗口,然后再次打开。 Also, I added some Thread.Sleep in order not to get errors because Web-Driver acts so fast this causes a problem. 另外,我添加了一些Thread.Sleep以便不会出错,因为Web-Driver动作如此之快会导致问题。

Updated Version of my Code: 我的代码的更新版本:

for (int i = 0;; i < stopLimit.Count; i++)
{
     try
     {
          Thread.Sleep(500);
          string score = Driver.FindElementByCssSelector("#parts > tbody > 
                                                   tr:nth-child(2) > td.score").Text;
          firstHalf[i] = score.Replace(" ", "");
     }
     catch(Exception)  
     {
          i--;
          Thread.Sleep(1500);
          Driver.SwitchTo().Window(Driver.WindowHandles.Last());
          Driver.Close();
          Driver.SwitchTo().Window(Driver.WindowHandles.First());
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM