StaleElementReferenceException during ElementValueChanged event

28 views
Skip to first unread message

tomas....@gmail.com

unread,
Jan 20, 2023, 11:44:48 AM1/20/23
to Selenium Users
Basically, we are struggling a lot with the speed of our testing infrastructure, which is 6-10 times slower than local execution. During the last few days, we started observing random cases of StaleElementReferenceException, with improved logging, I've tracked this issue to ElementValueChanged logging event's method while obtaining attribute values. Seems like somehow the page is updated faster before the whole communication is done and logging fails.

I'm actually not sure what is the best approach here. I think handling the exception is a way, but with that, I'll lose any info valuable for logging. Is there any way to back up the whole element object from DOM before it's removed? Used code:

```
var eventDriver = new EventFiringWebDriver(_driver);
eventDriver.ElementValueChanged += new EventHandler<WebElementValueEventArgs>(ElementChangedHandler);
```
```
        private void ElementChangedHandler(object sender, WebElementValueEventArgs e)
        {
            var s = new StringBuilder();
            s.Append(DateTime.Now + ": ");
            s.Append($"Changed element type '{e.Element.TagName}' ");
            s.Append($"id '{e.Element.GetAttribute("id")}' ");
            s.Append($"name '{e.Element.GetAttribute("name")}' ");
            s.Append($"auto-id '{e.Element.GetAttribute("data-test-id")}' ");
            s.Append($"to '{e.Value}'");
            LogItem(this, s.ToString());
        }
```
Reply all
Reply to author
Forward
0 new messages