1

I am new to Selenium WebDriver. I am stuck in locating one element for the following input text.

HTML code is

<input type="text" tabindex="1" size="30" name="flights_search[from_location_name]" id="flights_search_from_location_name" class="flight-from ui-autocomplete-input" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true">
<input id="flights_search_from_location" class="wego-from-code" type="hidden" name="flights_search[from_location]" value="">

Code I tried :

  • By ID

    driver.findElement(By.id("flights_search_from_location_name")).clear();
    
  • By xpath

    driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/form/fieldset/span[2]/input[1]")).clear();
    

I have tried with id, name, classname and xpath, but it is not able to locate this element.

Andersson
  • 51,635
  • 17
  • 77
  • 129

1 Answers1

1

Try to wait until required input field appears in DOM:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("flights_search_from_location_name"))).clear();

If element still couldn't be located, check whether input field located inside an iframe. If so, you need to switch to iframe to be able to handle elements inside it

Community
  • 1
  • 1
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • I tried by adding wait to input field below exception coming "org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for presence of element located by: By.id: flights_search_from_location_name" and also this element is not located inside iframe – Priya Verma Feb 20 '17 at 13:32
  • Can you share page `URL` or it's confidential? – Andersson Feb 20 '17 at 13:34
  • I am trying to Automate Booking Flight page in this demo Website. http://phptravels.net/flightsw and in this 'from location" input field – Priya Verma Feb 20 '17 at 13:34
  • Yes, `input` located inside an `iframe`. Try to switch to it with `driver.switchTo().frame(driver.findElement(By.tagName("iframe")));` – Andersson Feb 20 '17 at 13:36
  • @PriyaVerma Try to increase the time from 10 to 20 and also make sure that the element is available. For that you can inspect the element in the browser and check for the id. – Ravi MCA Feb 20 '17 at 13:36
  • Oh Thanks @Andersson. I didn't notice the ifame. Thanks for your help – Priya Verma Feb 20 '17 at 13:38
  • @PriyaVerma, Welcome. Please mark this answer as `Accepted` if it solved your problem. Thanks – Andersson Feb 20 '17 at 13:39