Getting StaleElementReferenceException: Element is no longer attached to the DOM

825 views
Skip to first unread message

anuj garg

unread,
Jul 27, 2015, 12:10:06 AM7/27/15
to Selenium Users
Hello All,

I am getting exception as "Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM" when I tried to find and click on an element on the website named "www.abhibus.com". Please help me out. Below is the code:

driver=new FirefoxDriver();
        driver.get("https://www.abhibus.com");
        driver.manage().window().maximize();
       
        element=driver.findElement(By.id("source"));
        element.sendKeys("ind");
       
        Thread.sleep(2000);
       
        List<WebElement>ls=driver.findElements(By.xpath("//ul[@id='ui-id-1']//li"));
       
        for(WebElement l: ls){
            if(l.getText().equals("Indore"))
                l.click();
           
        }
   
   
   
    element=driver.findElement(By.id("destination"));
    element.sendKeys("pu");
    Thread.sleep(2000);
    List<WebElement>ls1=driver.findElements(By.xpath("//ul[@id='ui-id-2']//li"));
   
    for(WebElement l1: ls1){
        if(l1.getText().equals("Pune"))
            l1.click();
       
    }
   
    driver.findElement(By.id("datepicker1")).click();
   
    Thread.sleep(2000);
   
    List<WebElement>ls2=driver.findElements(By.xpath("//div[@class='ui-datepicker-group ui-datepicker-group-first']//table//tbody//tr//td//a"));
     for(WebElement l2:ls2){
         if(l2.getText().equalsIgnoreCase("28")){
             l2.click();
         }
       
     }
driver.findElement(By.xpath(".//*[@id='roundTrip']/a")).click();   // for this line of code I am getting the StaleElementReferenceException
  

Sonia saini

unread,
Jul 27, 2015, 2:57:19 AM7/27/15
to seleniu...@googlegroups.com
You get StaleElementReferences exception when your element is not visible to selenium
Try to grab element first then perform action on it. 
Sudo code:

Grab element 

wait.Until(ExpectedConditions.ElementExists(By.XPath(Your element path));

 

Then perform action on element


element = browser.FindElement(By.XPath(your element path);



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/debebd51-f00d-4e64-962b-4a7f57876a83%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Sonia saini

Mercious

unread,
Jul 27, 2015, 3:23:14 AM7/27/15
to Selenium Users, sonia...@gmail.com
You might also want to try By.className("icosearch") instead.

Kaleem Uddin Mohammed Abdul

unread,
Jul 27, 2015, 4:03:55 AM7/27/15
to Selenium Users, anuj8...@gmail.com
The problem is here, their is no break statement in foreach loop. Because of this, the code is trying to get the innertext even their is no date widget is available (after clicking on 28th day). The below code works fine, I have tested in Java in C# :).
_WebDriver=new FirefoxDriver();
        _WebDriver.navigate().to("https://www.abhibus.com");
        _WebDriver.manage().window().maximize();
       
        WebElement element=_WebDriver.findElement(By.id("source"));
        element.sendKeys("ind");
       
        Thread.sleep(2000);
       
        List<WebElement>ls=_WebDriver.findElements(By.xpath("//ul[@id='ui-id-1']//li"));

       
        for(WebElement l: ls){
            if(l.getText().equals("Indore"))
            {
                l.click();
                break;
            }
           
        }
        WebElement Destination=_WebDriver.findElement(By.id("destination"));
        Destination.sendKeys("pu");
        Thread.sleep(2000);
        List<WebElement>ls1=_WebDriver.findElements(By.xpath("//ul[@id='ui-id-2']//li"));

       
        for(WebElement l1: ls1){
            if(l1.getText().equals("Pune"))
            {
                l1.click();
                break;
            }
           
        }
       
        _WebDriver.findElement(By.id("datepicker1")).click();
       
        Thread.sleep(2000);
       
        List<WebElement>ls2=_WebDriver.findElements(By.xpath("//div[@class='ui-datepicker-group ui-datepicker-group-first']//table//tbody//tr//td//a"));

         for(WebElement l2:ls2){
             if(l2.getText().equalsIgnoreCase("28")){
                 l2.click();
                 break;
             }
           
         }
         Thread.sleep(2000);
         _WebDriver.findElement(By.xpath("//*[@id='roundTrip']/a")).click();

anuj garg

unread,
Jul 27, 2015, 11:36:31 AM7/27/15
to Selenium Users, makale...@gmail.com
@Kaleem Thanks !!  break statement worked for me.But again one question why it is showing StaleElement reference Exception for Date picker only?Why not for source and destination station because for both of these I have also used for each loop to select values,and it is equally possible for these two fields also that the code will try to get the innertext in the autocomplete table for source and destination even if Autocomplete does not exists after selection of any value.Could you please explain why it is not showing StaleElement reference Exception for source and destination?

Kaleem Uddin Mohammed Abdul

unread,
Jul 27, 2015, 1:21:14 PM7/27/15
to Selenium Users, anuj8...@gmail.com

Development team didn't implemented the code to disable or remove or unselect the autocomplete suggestion for Destination and Source. But they have made disabled and unselectable the Date picker. I have attached the screen shots for your reference.
-Kaleem.

On Monday, July 27, 2015 at 9:40:06 AM UTC+5:30, anuj garg wrote:
EvenAferSelectingIndoreInDOMTheAutoCompleteListIsPresentAndITIsEnabled.jpg
TableIsDisabledAndNotSelectable.jpg

anuj garg

unread,
Jul 27, 2015, 1:59:49 PM7/27/15
to Selenium Users, makale...@gmail.com
Thanks a lot!!

Mukesh otwani

unread,
Jul 28, 2015, 4:16:27 PM7/28/15
to Selenium Users, anuj8...@gmail.com


On Monday, July 27, 2015 at 9:40:06 AM UTC+5:30, anuj garg wrote:
Reply all
Reply to author
Forward
0 new messages