【问题标题】:Python with selenium: unable to locate element带有硒的Python:无法找到元素
【发布时间】:2019-05-05 12:42:13
【问题描述】:

我一直在尝试定位到以下元素:

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

它是一个搜索框

我尝试使用类名、xpath、css 选择器,但这些都不起作用。我仍然收到带有相应属性的“无法找到元素”。

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')

我什至尝试使用 xpath helper 来找出确切的 xpath,但它显示错误:

[INVALID XPATH EXPRESSION]

帮我解决这个问题。

提前致谢。

【问题讨论】:

  • 试试find_element_by_xpath("""/html/body/div[1]/div[2]/div[1]/div[4]/div/div[2]/div[1]/input""")

标签: python selenium-webdriver xpath css-selectors webdriverwait


【解决方案1】:

让我们设置隐式等待超时:

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

执行所有可能需要太多时间,需要等待才能保证整个元素。

更多信息: Wait Timeout Selenium

【讨论】:

    【解决方案2】:

    所需元素是Angular 元素,因此要定位元素,您必须诱导 WebDriverWait 以使 元素可点击,您可以使用以下任一方法解决方案:

    • 使用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']")))
      
    • 使用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')]")))
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    【讨论】:

    • 感谢您的宝贵时间。我尝试了您所说的所有方法,但仍然无法找到该元素。
    • @SaikumarK 您的评论对我也没有帮助。抱歉,答案并没有帮助您解决问题,但是我将如何根据这些反馈改进我的帖子?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多