I’m facing a really frustrating situation with WebDriver because I’m getting a StaleElementReference Exception. Based on WebDriver documentation, this exception might be displayed for this two situations:
- The element has been deleted entirely.
- The element is no longer attached to the DOM.
What am I trying to do?
I was trying to create a Unit Test to verify that TeamMentors applied filters works fine, so I wanted to go thru all the Checkboxes from the Pivot Technology panel, check them and verify that the Applied filter items were consistent with the number of technologies selected or checked.
From a programming standpoint, I wanted to loop thru all the technologies using the beauty syntax of the Lambda expressions. So every time you select a technology, then an asynchronous task is fired to add that technology in the Applied filter section. It seems like the action of checking the check box updates the reference of the check box list, so the next element in the list show an error.
In the previous code, I’m getting all the technologies and store them in the pivotItems variable. Then I loop thru all the technologies and find the check box and then click them. But since the first occurrence fires an event (and the check action might be changing the DOM), then the reference of pivotItems was updated and the second iteration shows an error. The problem here is tied to a use a Reference of a WebElement (so if the original object is modified, the reference will get that changes as well).
I found a thread in the WebDriver community, but it seems like there are not much hopes on getting a fix on that.
What to do now?
It’s funny to think that a situation that looks pretty simple can become really nasty. I will need to get some cups of coffee and perform some research on this :).
I found a solution that is not as elegant has using the easy-to-read syntax of lambda expressions, but at the end of the day if solves the issue as an alternative solution. Instead of using a For,I’m using a While loop operator. So once the click operation is performed, then the Pivot technology is found (getting the latest DOM changes)
And if you look at the TeamMentor’s applied filters section then we can see that all filters were loaded in the applied filters section.