0

I looked at the discussions on the online forums and on this website and added the wait function. However, it continues to show this error Here is the code for the problem:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.base.Function;

public class WebDriverNavigate {
    public static void main(String[] args) throws InterruptedException {
    System.setProperty("webdriver.gecko.driver",System.getProperty("user.dir")+"/GeckoDriver/geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("http://www.google.com");
        WebElement textbox = driver.findElement(By.id("lst-ib"));
        textbox.sendKeys("Search on Google");       
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(visibilityOfElementLocated(By.id("lst-ib")));
        driver.findElement(By.name("btnK")).click();;
        textbox.clear();                
        textbox.sendKeys("This is the second search");
        WebElement searchbutton2 = driver.findElement(By.id("fZl"));
        searchbutton2.click();
        driver.navigate().back();
        driver.navigate().forward();
        driver.navigate().refresh();

    }

    private static Function<WebDriver,WebElement> visibilityOfElementLocated(final By locator) {
        return new Function<WebDriver, WebElement>() {
            @Override
            public WebElement apply(WebDriver driver) {
                return driver.findElement(locator);
            }
        };
    }
}

Here is the HTML snippet:

<input class="gsfi" id="lst-ib" maxlength="2048" name="q" autocomplete="off" title="Search" value="Search on Google" aria-label="Search" aria-haspopup="false" role="combobox" aria-autocomplete="list" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; background: transparent url(&quot;data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D&quot;) repeat scroll 0% 0%; position: absolute; z-index: 6; left: 0px; outline: medium none;" dir="ltr" spellcheck="false" type="text">

Error message on the console:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: The element reference of stale: either the element is no longer attached to the DOM or the page has been refreshed

A N
  • 19
  • 8

3 Answers3

1

I was able to solve the problem by rearranging the code in this order:

        WebElement searchbutton = driver.findElement(By.name("btnK"));
        driver.findElement(By.name("btnK")).click();
        WebDriverWait wait = new WebDriverWait(driver,10);
        WebElement text2 = wait.until(visibilityOfElementLocated(By.id("lst-ib")));
        text2.clear();  

It looks like I was trying to wait before the click() but should have waited for the element to load after the click() but before the clear() Now it does not throw that error

Thank you all for your suggestions and solutions. I am sure they would be useful in future in a more complicated code problems.

A N
  • 19
  • 8
0

I have this problems many times, and sometimes it is imposible solved.

First you should find the element with the Wait or WaitFluent selenium classes. Because maybe you try find the element before it is yet loaded. Something like that:

new FluentWait<WebDriver>(driver)
            .withTimeout(IMPLICIT_TIMEOUT, TimeUnit.SECONDS)
            .pollingEvery(RETRY_TIME, TimeUnit.SECONDS)
            .ignoring(StaleElementReferenceException.class,
                      NoSuchElementException.class)
            .until(ExpectedConditions
            .visibilityOfElementLocated(By.xpath(xpath)));

Other way is try multiple times in a for with a driver.sleep() inside.

j.barrio
  • 1,006
  • 1
  • 13
  • 37
  • Thank you ! It seems my code was rearranged wrongly. I have put my answer on how I solved this particular issue. However, I know your suggestion would be useful in case I come across similar problem in the future. – A N Oct 13 '17 at 22:48
  • Your welcome. You can vote te answer, if they are useful. – j.barrio Oct 14 '17 at 21:00
0

Most of the time, this is due to the DOM being updated and you trying to access an updated/new element. So this Question&Answers should cover you.

You can also check Selenium Documentation.

In most cases explicit wait works.

or you can ignore the error until the element can be clickable

new WebDriverWait(driver, timeout).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(By.name("btnK")));
driver.findElement(By.name("btnK")).click();
  • I tried the code that you provided. However, it still shows the same error. I will try to see if the other solutions in *Questions&Answers* work. Thank you – A N Oct 13 '17 at 21:25
  • Thank you ! It seems my code was rearranged wrongly. I have put my answer on how I solved this particular issue. However, I know your suggestion would be useful in case I come across similar problem in the future. – A N Oct 13 '17 at 22:48