1

I am trying to click on an input field in the Website-link using xpath
usually it works for me for all urls.. but for this specific url whatever I try I cant click on the input field using selenium webdriver.

Webdriver loads the page but won't click on the element.
This is what I have tried so far: (it is a java project for test automation using Selenium webdriver)

System.setProperty("webdriver.gecko.driver", "C:\\Users\\nimal\\eclipse-workspace\\webdriver\\chromedriver.exe") ;
      WebDriver driver = new ChromeDriver();
       driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
       driver.get("https://www.kayak.com/flights");

       WebElement searchfield1 =  driver.findElement(By.xpath("//*[@id='c5NwV-origin-airport-display']")); 
       searchfield1.click();
       WebElement searchfield2 =  driver.findElement(By.xpath("//*[@id='c5NwV-origin-airport']")); 
       searchfield2.sendKeys("Toronto");

I tried to first click on the div element using xpath, as then only I get the input field.
Then tried to send "Toronto" to the input field using xpath.

Any help or suggestions are most welcome.

4
  • 1
    Your locator //*[@id='c5NwV-origin-airport'] returns no matches whereas //*[contains(@id,'-origin-airport')]returns 16 matches. Which one are you trying to sendkeys to? Commented Aug 9, 2018 at 16:48
  • 1
    Likewise //*[@id='c5NwV-origin-airport-display'] returns no matches for me. This is apparently a dynamically created ID and you'll need to narrow down the locator using additional information. Commented Aug 9, 2018 at 16:51
  • @Bill Hileman This is the first time i am dealing with Dynamic IDs, and Thanks for the help.This is what exactly i was looking for. Commented Aug 9, 2018 at 17:05
  • @Bill Hileman, It clicks properly when i select the first of the 16 possibilties, Thanks Commented Aug 9, 2018 at 17:08

1 Answer 1

3

Here, the id is getting changed dynamically.So, you need to find the element using other unqiue reference and you need to change your Xpath locator for the searchfield1 as //div[@class='search-form-inner']//div[@data-placeholder='From?']

searchfield2 element can be find using the name attribute. you need to perform clear action before sending the key.Since,some of the data is pre populated in the origin field

As a best practice add some wait after loading the URL for the page load completion.

Working Code:

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

WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.titleContains("Flights"));

WebElement searchfield1 =  driver.findElement(By.xpath("//div[@class='search-form-inner']//div[@data-placeholder='From?']"));
searchfield1.click();

wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("origin")));

WebElement searchfield2 =  driver.findElement(By.name("origin"));
searchfield2.clear();
searchfield2.sendKeys("Toronto");
Sign up to request clarification or add additional context in comments.

1 Comment

@Nimalkrishna: Glad to know that it helped!

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.