简体   繁体   English

带有硒的Python:无法找到元素

[英]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. 我尝试使用类名,xpath,css选择器,但这些都不起作用。 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: 我什至尝试使用xpath助手来查找确切的xpath,但是它显示错误为:

[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: 所需元素是Angular元素,因此要定位该元素,必须使WebDriverWait 变为可单击元素,并且可以使用以下任一解决方案:

  • Using CSS_SELECTOR : 使用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 : 使用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 有关更多信息: 等待超时硒

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

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