2

I'm creating List of all available elements with below Xpath.

IList<IWebElement> test= Driver.FindElements(By.XPath("//*[@id='middle-container']//div[@class='middle-section match-list']//div[contains(@class,'title')]//span[contains(text(),'" + Event.Trim() + "')]//..//..//..//..//div[contains(@class,'drop-down-content')]//table[contains(@class,'hidden-xs')]//tr//td[contains(@class,'bettype')]//a[@class='bet']`//span"));

So all the elements available in that Xpath need to be clicked. Running foreach loop:

foreach (var item in availableSports)
    {
        item.Click();        
    }
}

My problem is let's say if test contains more than, I think, 10 elements, it is stopping the click event after around 8 to 9 clicks, and raising this error:

StaleElementReferenceException

So just wondering how can I write the method which will continue click until last available element without fail.

Drenmi
  • 8,492
  • 4
  • 42
  • 51
vic
  • 217
  • 1
  • 7
  • 18
  • You can use wait() functions for Expected Condition. whatever element you expect to appear(or clickable etc) wait for it. – Mahsum Akbas Oct 12 '15 at 05:57

1 Answers1

0

You are getting StaleElementReferenceException because something has changed in the DOM after you performed the FindElements operation.

You have mentioned that you are clicking on the items in the list. Does this click action reload the page or navigate to a different page. In both cases the DOM has changed. Hence the Exception.

You can handle this(hopefully) with the following logic. I am a JAVA guy and the following code is in JAVA. But I think you get the idea.

IList<IWebElement> test= Driver.FindElements(By.XPath("//*[@id='middle-container']//div[@class='middle-section match-list']//div[contains(@class,'title')]//span[contains(text(),'" + Event.Trim() + "')]//..//..//..//..//div[contains(@class,'drop-down-content')]//table[contains(@class,'hidden-xs')]//tr//td[contains(@class,'bettype')]//a[@class='bet']`//span"));
// Instead of using the for each loop, get the size of the list and iterate through it
for (int i=0; i<test.length; i++) {
    try {
        test.get(i).click();
    } catch (StaleElementReferenceException e) {
        // If the exception occurs, find the elements again and click on it
        test = test= Driver.FindElements(By.XPath("//*[@id='middle-container']//div[@class='middle-section match-list']//div[contains(@class,'title')]//span[contains(text(),'" + Event.Trim() + "')]//..//..//..//..//div[contains(@class,'drop-down-content')]//table[contains(@class,'hidden-xs')]//tr//td[contains(@class,'bettype')]//a[@class='bet']`//span"));
        test.get(i).click();
    }
}

Hope this helps you.

StrikerVillain
  • 3,719
  • 2
  • 24
  • 41
  • Hi @Striker , thanks for answering but i dont want to click the same element again. If i use the above code then it will try to click until exception occur but as soon as it occurred it will find the element again and try to click all the element again and that will give me error on UI, saying Alreay in a basket . So Want to avoid it and click only whichever were not clicked in a first place , – vic Oct 13 '15 at 00:01
  • It does not do that... As u can see we iterate `i` from 0 to the number of elements ... So the element is not clicked twice... When an element is having exception, the elements are found again and the element at `i` is clicked – StrikerVillain Oct 13 '15 at 00:47
  • Thanks for the explanation. But I need that in c# as i cant be able to convert it into c# . So can any one able to help me to convert it to c#? – vic Oct 13 '15 at 01:06
  • c# code for (int i=0; i – vic Oct 13 '15 at 02:17