简体   繁体   中英

Python with selenium: unable to locate element

I have been trying to locate to the following element:

<input type="text" ng-change="vm.refreshTimeline()" button-enter="vm.filterRowChange();" ng-model="vm.filterRowValue" class="ng-pristine ng-untouched ng-valid ng-empty">

Its is a search box .

I tried using class name, xpath, css selector but none of these are working. Still i am getting " Unable to locate the element " with the respective attributes.

driver.find_element_by_css_selector('body > div:nth-child(1) > div.uiview.ng-scope > div.container.asset-details.ng-scope > div.row.top-buffer > div > div.GanttController > div.input-append.text-center.search_section.ng-scope > input')

driver.find_element_by_class_name('ng-pristine ng-untouched ng-valid ng-empty')

  find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/div[4]/div/div[2]/div[1]/input')

find_element_by_link_text('Search by Equipment ID or S/N')

I even tried using xpath helper to find out the exact xpath, but it is showing an error as:

[INVALID XPATH EXPRESSION]

Help me to overcome this issue.

Thanks in advance.

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 solutions:

  • Using CSS_SELECTOR :

     element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ng-pristine.ng-untouched.ng-valid.ng-empty[ng-change*='refreshTimeline']"))) 
  • Using XPATH :

     element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-pristine ng-untouched ng-valid ng-empty' and contains(@ng-change,'refreshTimeline')]"))) 
  • 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 

Lets Set Implicit Wait Timeout:

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

It maybe take too much time to perform all and it needs to wait in order to guarantee the whole of elements.

For more information: Wait Timeout Selenium

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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