I am facing an issue with the following webdriver function:
this.clickButton = async function () {
try {
var buttonElement = await driver.findElement(By.className('button-class'));
await buttonElement.click();
}
catch (err) {
console.log(err);
}
}
Occasionally, I encounter a Stale Element exception
when using this function.
Even after modifying it to:
this.clickButton = async function () {
try {
await driver.findElement(By.className('button-class')).click();
}
catch (err) {
console.log(err);
}
}
I have some questions regarding this situation:
Is it common for a
Stale Reference exception
to occur in a scenario like this, where the element reference is obtained and then immediately utilized in the subsequent line of code with no additional page interactions? (I would expect an 'element not found' exception if there was no 'button-class' element, but it seems odd that the element exists when searched for, only to become stale by the time the next line of code is executed.)If the answer to question 1 is affirmative, how is this possible? The found element is promptly acted upon without any delay, similar to what is happening here. It's worth noting that there is no reusing of locators or elements; the function searches for the element each time it is called and completes its execution right after the click event.
Could the fact that clicking the button removes it from the page be relevant to this problem? In other words, is it conceivable that I am encountering this exception because the button disappears post-click operation?