'Stackelementreference exception in if (Citi.equals("MAA"))

I tried to automate the MakeMyTrip site ,I am getting a stale element reference error in line number 42 which i have highlighted .. I have tried all possible solutions..could u please help me with it ..

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.concurrent.TimeUnit;

import java.util.stream.Stream;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.StaleElementReferenceException;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

import net.bytebuddy.asm.Advice.Return;

public class Dropdown {

@SuppressWarnings("deprecation")

public static void main(String[] args) throws InterruptedException {

    System.setProperty("webdriver.chrome.driver", "C:\\Users\\arthi\\ChromeDriver\\chromedriver.exe");

    WebDriver driver = new ChromeDriver();

    Actions act = new Actions(driver);

    driver.get("https://www.makemytrip.com/");


    driver.manage().window().maximize();

    WebElement Fromcity = driver.findElement(By.id("fromCity"));

    act.doubleClick(Fromcity).perform();

    Thread.sleep(1000);

    WebElement From = driver.findElement(By.xpath("//*[@placeholder='From']"));

    act.doubleClick(From).perform();


    From.sendKeys("chenn");

//List Fromcities=driver.findElements(By.xpath("//*[@id='react-autowhatever-1']/div[1]/ul/li/div/div[1]/p"));

    List<WebElement> Fromcities = driver.findElements(By.xpath("//ul[@role='listbox']//li/div[1]/div[2]"));

    for (WebElement Cities : Fromcities)

    {

        String Citi = Cities.getText();

        **if (Citi.equals("MAA"))** {

            try {

                Cities.click();

            } catch (StaleElementReferenceException ex) {

                Fromcities = driver

                        .findElements(By.xpath("//*[@id='react-autowhatever-1']/div[1]/ul/li/div/div[1]/p"));

                Cities.click();

            }

        }

    }

//***to path

    driver.findElement(By.xpath("//input[@placeholder='To']")).click();

    WebElement ToPlace = driver.findElement(By.xpath("//input[@placeholder='To']"));

    act.doubleClick(ToPlace).perform();

    ToPlace.sendKeys("mum");

    List<WebElement> ToCities = driver.findElements(By.xpath("//ul[@role='listbox']//li/div[1]/div[2]"));

    for (WebElement To : ToCities)

    {

        if (To.getText().contains("BOM")) {

            try {

                JavascriptExecutor executor = (JavascriptExecutor) driver;

                executor.executeScript("arguments[0].click();", To);

            } catch (StaleElementReferenceException e) {

                ToCities = driver.findElements(By.xpath("//ul[@role='listbox']//li/div[1]/div[2]"));

                To.click();

            }

            List<WebElement> Radiobutton = driver

                    .findElements(By.xpath("//ul[@class='specialFareNew']/li/div[2]/p"));

            for (WebElement Radio : Radiobutton) {

                if (Radio.getText().contains("Armed Forces Fares")) {

                    Radio.click();

                    System.out.println("got the expected output");

                    break;

                }

            }

        }

    }

}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source