Stale element reference on selecting checkout date in Airbnb site

184 views
Skip to first unread message

Savan Rai

unread,
Apr 1, 2018, 9:59:51 AM4/1/18
to Selenium Users
Stale element reference on selecting checkout date in Airbnb site.

I am able to select checkin date.But stale element error is encountered on selection of checkout date.
Please help in fixing this issue.

Code:

package test.java.tests;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class DatePicker {
 public static void main(String[] args){
     
    System.setProperty("webdriver.chrome.driver", "/Users/savanrai/Documents/Jars/chromedriver");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.airbnb.com.sg/a");   
    driver.manage().window().fullscreen();   
    driver.findElement(By.id("checkin_input")).click();   
    List<WebElement> date = driver.findElements(By.xpath("//div[@class='_1lds9wb']//div[@class='_gahfr9']//table[@class='_p5jgym']//td"));
   
    for(int j=0;j<date.size();j++){
        String dateFrom = date.get(j).getText();
        System.out.println(dateFrom);
        if(dateFrom.equals("7")){
            date.get(j).click();
        }
    }
   
    for(int j=0;j<date.size();j++){
        String dateFrom = date.get(j).getText();
        System.out.println(dateFrom);
        if(dateFrom.equals("8")){
            date.get(j).click();
        }
    }
 }
}


Error:
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=65.0.3325.181)
  (Driver info: chromedriver=2.35.528157 (4429ca2590d6988c0745c24c8858745aaaec01ef),platform=Mac OS X 10.13.3 x86_64) (WARNING: The server did not provide any stacktrace information)

Xiang Dong

unread,
Apr 1, 2018, 8:42:47 PM4/1/18
to Savan Rai, Selenium Users
Get a sleep or wait for a while and reallocate the element. DOM get changed from your previous action. It is normal when you try to play action in an iteration.

Best Regards,
--david

From: seleniu...@googlegroups.com <seleniu...@googlegroups.com> on behalf of Savan Rai <sava...@gmail.com>
Sent: Sunday, April 1, 2018 8:30 PM
To: Selenium Users
Subject: [selenium-users] Stale element reference on selecting checkout date in Airbnb site
 
--
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/34c3f971-56e0-4e4d-8da3-b97dc6bcb24e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vishal Kumar

unread,
Apr 2, 2018, 4:06:57 AM4/2/18
to seleniu...@googlegroups.com

Hi,

Code:

package test.java.tests;

import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class DatePicker {
 public static void main(String[] args){
     
    System.setProperty("webdriver.chrome.driver", "/Users/savanrai/Documents/Jars/chromedriver");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.airbnb.com.sg/a");    
    driver.manage().window().fullscreen();    
    driver.findElement(By.id("checkin_input")).click();    
    List<WebElement> date = driver.findElements(By.xpath("//div[@class='_1lds9wb']//div[@class='_gahfr9']//table[@class='_p5jgym']//td"));
WebDriverWait wait=new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.visibilityOfAllElements(date));
    
    for(int j=0;j<date.size();j++){
        String dateFrom = date.get(j).getText();
        System.out.println(dateFrom);
        if(dateFrom.equals("7")){
            date.get(j).click();
        }
    }
    
    for(int j=0;j<date.size();j++){
        String dateFrom = date.get(j).getText();
        System.out.println(dateFrom);
        if(dateFrom.equals("8")){
            date.get(j).click();
        }
    }
 }
}

Use this one, i hope this will be work.

Thanks,
Vishal

--
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-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.

Magesh N

unread,
Apr 2, 2018, 5:42:24 AM4/2/18
to Selenium Users
Just reinitialize the element which throws stale element
Reply all
Reply to author
Forward
0 new messages