WebDriver.getAccessibleName() throws StaleElement Exception using Selenium

351 Views Asked by At

I have a simple <a> element which is inside an iframe. I want to click it and if I first try to use getAccessibleName() method on it and then click it I got StaleElementReferenceException. If I try to direct click the element there isn't any problems. At the same time I am able to use methods like getText() or getLocation(), but If I try call getAccessibleName() then unknow error is provided and 32000 is the code. After that the element becoms stale and can't be used anymore.

Where is the problem and why this method provide such an error? I'm using this method to create something like a detailed report for test execution and is important for me. I don't have any other problems and I using this method all the time and this is the first case with errors? If someone else is having an issue like this please let me know, will appreciate all comments and suggestions.

1

There are 1 best solutions below

0
undetected Selenium On

getAccessibleName()

getAccessibleName() gets result of a Accessible Name and Description Computation for the Accessible Name of the WebElement.

Ideally before extracting the Accessible Name you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use the following solution:

driver.get("https://www.selenium.dev/");
System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[text()='Getting Started']"))).getAccessibleName());

Console Output:

Getting Started