0

I need to find an element in this area, but every single time id and name changing, also XPath too. Can't use classes as well cause there is two fields got same classes:

driver.find_element_by_xpath('//*[@id="flightDepartureSearchAutoComplate3-0-69f"]').send_keys('AYT')

HTML:

<input name="flightDepartureSearchAutoComplate3-0-69f" id="flightDepartureSearchAutoComplate3-0-69f" type="text" ng-model="departureFlightSelectedDestination0" placeholder="From" class="form-control auto-complete ng-pristine ng-valid ng-valid-editable ng-empty ng-touched" uib-typeahead="departureFlightDestination as departureFlightDestination.label for departureFlightDestination in controller.getDepartureAutocomplete($viewValue,0) | limitTo: 10" typeahead-editable="false" typeahead-wait-ms="750" typeahead-min-length="3" typeahead-append-to="departureTargetElements[0]" typeahead-select-on-blur="true" typeahead-focus-on-select="false" typeahead-select-on-exact="true" typeahead-template-url="flightAutoComplateTypeaheadTemplate.html" typeahead-on-select="controller.departureAutoComplateChanged()" autocorrect="off" autocapitalize="off" autocomplete="off" spellcheck="false" b2b-validate="required" data-sln-id="slnTxt_departureFlightAutoComplate" autofocus="" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-25-8460" style=""> 

Error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="flightDepartureSearchAutoComplate3-0-22d"]"}
  (Session info: chrome=73.0.3683.103)
  (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17134 x86_64)
Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Ali Özyer
  • 11
  • 2
  • How many elements are there on the page that start with flightDepartureSearchAutoComplate for the id? Is it only one? – QHarr Apr 24 '19 at 05:48

1 Answers1

1

The desired element is an Angular element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control.auto-complete.ng-valid-editable[id^='flightDepartureSearchAutoComplate'][placeholder='From']"))).send_keys('AYT')
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='form-control auto-complete ng-pristine ng-valid ng-valid-editable ng-empty ng-touched' and starts-with(@id, 'flightDepartureSearchAutoComplate')][@placeholder='From']"))).send_keys('AYT')
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Why Angular element implies inducing WebDriverWait? – Mate Mrše Apr 24 '19 at 06:55
  • @MateMrše Angular element implies those are created/modified by _JavaScript_ and/or _AJAX Calls_. So _WebDriverWait_ is inevitable in order to interact with those elements. – undetected Selenium Apr 24 '19 at 07:10
  • @DebanjanB According to your recommend i have errors below raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – Ali Özyer Apr 27 '19 at 10:05
  • Its solved, with: driver.find_element_by_xpath('//input[@placeholder="From"]').send_keys('AYT') – Ali Özyer Apr 27 '19 at 10:36