Issue
I am trying to automate the following actions:
- Launching https://www.flipkart.com > Click on Mobiles > Mouse hover on Electronics and then click on Mi.
I am getting Expection in thread "main" state Element Reference: element is not attached to the page document in the miButton() method.
Please refer the error details section.
HTML Code
Base class:
public class Base {
static WebDriver driver;
public void setupBrowser(String browser, String url) {
String currDir = System.getProperty("user.dir");
if(browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", currDir + "\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browser.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver", currDir + "\\drivers\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("edge")) {
System.setProperty("webdriver.edge.driver", currDir + "\\drivers\\msedgedriver.exe");
driver = new EdgeDriver();
}
else {
System.out.println("Valid browser not found therefore quitiing");
System.exit(0);
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if(url != "")
driver.get(url);
else
driver.get("about:blank");
}
public void closeBrowser() {
driver.close();
}
Page Class
public class pagetest extends Base{
Actions action;
public void closebtn() {
driver.findElement(By.cssSelector("button._2doB4z")).click();
}
public void mibutton() {
WebElement mobiles = driver.findElement(By.xpath("//div[text()='Mobiles']"));
action = new Actions(driver);
action.moveToElement(mobiles).click().perform();
WebElement electronicsmenu = driver.findElement(By.xpath("//span[text()='Electronics']"));
action.moveToElement(electronicsmenu).click().perform();
List <WebElement> value = driver.findElements(By.xpath("//div[@class='_1kidPb']/div[@class='_1QrT3s']//a");
for(WebElement elem:value) {
if(elem.getText().equals("Mi")) {
elem.click();
}
}
WebElement label = driver.findElement(By.xpath("//p[text()='Latest from MI : ']"));
System.out.println("The Label 'Latest from MI' is present : " +label.isEnabled());
}
public static void main(String[] args) {
pagetest obj = new pagetest();
obj.setupBrowser("chrome", "https://www.flipkart.com/");
obj.closebtn();
obj.mibutton();
}
}
Error Details
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=89.0.4389.90)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html
Solution
You never find elements and then right away try to click on them. When the page is loading the elements are created, but after a few milliseconds they change their attributes, size and or location. This exception means that you found the element but as of now you "lost" it.
In order to avoid StateElementReferecneException use WebDriverWait object. You can wait for element to be visible / clickable as well as other options. Best option is to wait for the element to be clickable:
WebDriverWait wait = new WebDriverWait(driver, 20, 10);
wait.until(ExpectedConditions.elementToBeClickable
(By.id("elementID")));
Answered By – Tal Angel
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0