4

I am trying to click on dropdown value to select city in from field in Make my trip http://www.makemytrip.com/. But getting Stale element reference exception. Ids are getting changed on page load. Tried below code:

driver.findElement(By.xpath(".//*[@id='hp-widget__sfrom']")).clear();
driver.findElement(By.xpath(".//*[@id='ui-id-1']"));
driver.findElement(By.xpath(".//*[@id='hp-widget__sfrom']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeSelected(driver.findElement(By.xpath(".//*[@class='ui-menu-item'][2]"))));
2
  • plz provide total exception faced to explain better why it is happen. Commented Jul 7, 2018 at 17:24
  • Thanks @muraliselenium for your response, but it is resolved now. Commented Jul 8, 2018 at 14:00

4 Answers 4

2

To click on a dropdown value e.g. Mumbai you can use the following solution:

  • Code Block:

    driver.get("https://www.makemytrip.com/")
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='input_fromto checkSpecialCharacters ui-autocomplete-input' and @id='hp-widget__sfrom']"))).click();
    List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//li[@class='ui-menu-item'][starts-with(@id,'ui-id-')]//span[@class='autoCompleteItem__label']")));
    for (WebElement element:myList)
        if(element.getText().contains("Mumbai"));
            element.click();
    
  • Browser Snapshot:

from_mumbai

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply, just wanted to understand, why do I need to give class name in xpath in 'From' when id of From is unique.
@user2377826 So basically your question is why we have used an xpath when an id is present. In short using class name, id, name or tag name is converted into css selector by the Java Client by default. So the choice is between using css selector or xpath. As your code trials were based on xpath, hence I did so. You can find a detailed discussion in Official locator strategies for the webdriver
0

You can use this working code:

WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");

driver.findElement(By.xpath(".//*[@id='hp-widget__sfrom']")).clear();
driver.findElement(By.xpath(".//*[@id='hp-widget__sfrom']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class='ui-menu-item'][2]/div/p[1]/span[1]"))).click();

I have fixed the xPath of dropdown list element. Always try to specify the exact element yo want to interact with. For example if you want to click on button, try to find <span> or <button> tag, for a link <a> tag and for input fields <input> tag.

Comments

0

You can try this code :

I do not see any use of xpath in this scenario. I have converted some of the xpath to either css selector or id. and have kept only one. Though I have not faced any stale element reference.

System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver.exe");
        driver  = new ChromeDriver();
        driver.manage().window().maximize();
        WebDriverWait wait = new WebDriverWait(driver, 30);
        driver.get("https://www.makemytrip.com/");
        WebElement from =  wait.until(ExpectedConditions.elementToBeClickable(By.id("hp-widget__sfrom")));
        from.click();
        from.clear();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("ul[class*='ui-widget-content hp-widget__sfrom']")));
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[contains(@aria-label,'Top Cities : Mumbai, India ')]"))).click();

Comments

0

The below code works fine for me and it is parameterized as well, it works for any input value without changing the xpath. In this example, I took mumbai as test data.

    driver.get("https://www.makemytrip.com/");
    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    driver.findElement(By.xpath("//input[contains(@id,'hp-widget__sfrom')]")).clear();
    driver.findElement(By.xpath("//input[contains(@id,'hp-widget__sfrom')]")).click();
    driver.findElement(By.xpath("//input[contains(@id,'hp-widget__sfrom')]")).sendKeys("Mumbai");
    Thread.sleep(2000);
    WebDriverWait wait = new WebDriverWait(driver, 30);
    By option = By.xpath("//div[@class='autoCompleteItem']/p/span[contains(text(),'Mumbai')]");
    wait.until(ExpectedConditions.elementToBeClickable(option));
    driver.findElement(option).click();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.