0

I am trying to take some scores from websites while using webdriver. I tried so far XPath, CSS, Classname but, sometimes it is located the item, sometimes it does not.

This is the HTML code that I have been trying to take it:

<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.

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();
                        }
                    }
                }
5
  • Which element u want to locate Commented Sep 16, 2017 at 10:49
  • @iamsankalp89 I want to locate class score or child classes and these are p1_home and p1_away Commented Sep 16, 2017 at 11:01
  • //span[@class='p1_home'] and //span[@class='p1_away'] Commented Sep 16, 2017 at 11:01
  • You'd better use ExpectedConditions class instead of sleeping. In that case webdriver will check every 0.5 sec if element is loaded and will fetch it as soon as it appears. Commented Sep 16, 2017 at 12:08
  • 2
    It seems like the page you are using has some js which modifies the html. How to wait for pages to load this type of content is certainly a point of discussion among automation / test devs. Commented Sep 16, 2017 at 12:10

3 Answers 3

1

You can use

//span[@class='p1_home']

//span[@class='p1_away']
Sign up to request clarification or add additional context in comments.

4 Comments

I am trying write now, and I will test whether the program crash or not
Ok try and let me know what happens
Okay, I tried it. On the first run, it worked perfectly, but on the second run, it crashed
Give some wait and then try
1

To locate element of particular class and attribute you can use CSS selector. 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']")));

2 Comments

I am testing other solution. I will let you know whether works or not later
Added alternative option, to address possible loading time issues.
0

Okay, I found a solution with a different approach. Instead of getting two specific class, I used parent class score. Also, I deleted the "for loop" and "try" and "catch" which I am using for finding the object again and again.

To my main loop, I added "try" and "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.

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());
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.