Search code examples
user-interfaceselenium-webdrivertestingjunit

Test lufthansa, element is not found although XPATH exists and was copied from the inspector tools by right clicking on the page and accessing HTML


I am attempting to write a Selenium script to test the Lufthansa website. I want to enter "RBA" as the origin airport and "JFK" as the destination airport. However, I am receiving the following error message despite copying the XPATH by right-clicking on the element in the inspector tools:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode"]"}
      (Session info: chrome=114.0.5735.110)

This is the code I am using:

package Test;
// Generated by Selenium IDE
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;

public class AirLineTest {
    private WebDriver driver;
    private Map<String, Object> vars;
    JavascriptExecutor js;

    @Before
    public void setUp() {
        driver = new ChromeDriver();
        js = (JavascriptExecutor) driver;
        vars = new HashMap<String, Object>();
    }

    @After
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void test() {
        driver.get("https://www.lufthansa.com/ma/en/homepage");
        driver.manage().window().setSize(new Dimension(1552, 840));
        driver.findElement(By.xpath("//*[@id=\"cm-acceptAll\"]")).click();
        driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode\"]")).clear();
        driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode\"]"))
              .sendKeys("RBA"); 
        driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].destinationCode\"]"))
              .sendKeys("JFK"); 
        driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].travelDatetime\"]"))
              .click();
        driver.close(); 
    }
}

Solution

  • Root Cause: This XPath expression //*[@id="dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode"] which you are using to locate the element's ID attribute is incorrect, because value of ID is dynamic. Each time the page loads the value is different. Hence change your XPath expression as shown below:

    Change these lines:

    driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].destinationCode\"]"))
                  .sendKeys("JFK"); 
    

    To:

    driver.findElement(By.xpath("//input[@placeholder='From']")).clear();
    driver.findElement(By.xpath("//input[@placeholder='To']")).sendKeys("JFK");