简体   繁体   English

如何使用Selenium和Python查找此示例的元素

[英]How to find element for this example using Selenium and Python

I need to find an element in this area, but every single time id and name changing, also XPath too. 我需要在该区域中找到一个元素,但是每次更改ID和名称时,XPath也会发生变化。 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: 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)

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 : 所需的元素是Angular元素,因此要定位该元素,必须诱导WebDriverWait才能使该元素可单击,并且可以使用以下任一Locator Strategies

  • Using CSS_SELECTOR : 使用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 : 使用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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM