【问题标题】:How to locate the element for the below html using Selenium and Python如何使用 Selenium 和 Python 定位以下 html 的元素
【发布时间】:2019-04-10 05:43:11
【问题描述】:

我正在尝试为以下 html 标记查找 xpath。但无法在 selenium 中找到。因为我是初学者,所以我需要一些帮助和建议。出现错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='react-autosuggest__input react-autosuggest__input--open']"}
(Session info: chrome=73.0.3683.103)

这是 HTML 元素。

<input type="text" autocomplete="off" aria-autocomplete="list" aria-controls="react-autowhatever-1" class="react-autosuggest__input react-autosuggest__input--open" placeholder="From" value="">

我已经尝试为上面的 HTML 制作了这个 xpath。但是同样会抛出错误。

//*[@class='react-autosuggest__input react-autosuggest__input--open']   

我希望上述 xpath 的输出能够定位 selenium 中的元素,但实际上我遇到了错误。

【问题讨论】:

  • //input[@class='react-autosuggest__input react-autosuggest__input--open'] 你试过这种方式吗? ?请确认是否存在具有相同类名的其他输入类型。
  • 嗨@Dhru'soni。我刚刚试过这个。但得到一些错误。 “消息:无效选择器:无法使用 xpath 表达式定位元素 //input[@class='react-autosuggest__input react-autosuggest__input--open'] '] 因为以下错误:SyntaxError: 无法执行'评估'关于 'Document':字符串 '//input[@class='react-autosuggest__input react-autosuggest__input--open'] ']' 不是有效的 XPath 表达式。”

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

元素似乎是 React 元素,因此要定位元素,您必须诱导 WebDriverWait 以使 元素可点击,您可以使用以下解决方案:

  • 使用CSS_SELECTOR

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.react-autosuggest__input.react-autosuggest__input--open[placeholder='From']")))
    
  • 使用XPATH

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='react-autosuggest__input react-autosuggest__input--open' and @placeholder='From']")))
    
  • 注意:您必须添加以下导入:

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

【讨论】:

  • 嗨@DebanjanB 感谢您的解决方案。我已经尝试了这两种解决方案。但出现以下错误。“回溯(最后一次调用): from_field = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[ @class='react-autosuggest__input react-autosuggest__input--open' 和 @placeholder='From']"))) 文件 "C:\Users\Anurag Jangde\PycharmProjects\Selenium_python\venv\lib\site-packages\selenium\ webdriver\support\wait.py",第 80 行,直到引发 TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:"
  • TimeoutExceptionfailed expected-conditions 的结果。通过find_element_by_*time.sleep() 一起调试您的代码。如果您能够找到该元素,请使用观察结果更新问题。
  • 嗨@DebanjanB 我无法通过这两种解决方案找到元素。感谢您的帮助!
  • 检查元素是否在&lt;iframe&gt;
【解决方案2】:

尝试使用 WebDriverWait,只设置更长的等待时间,例如:

element = WebDriverWait(driver, 60).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='From']")))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2021-05-03
    • 2021-10-25
    • 2017-06-19
    • 1970-01-01
    相关资源
    最近更新 更多