【问题标题】:Unable to locate Search bar element in Facebook with XPATH无法使用 XPATH 在 Facebook 中找到搜索栏元素
【发布时间】:2020-05-20 06:10:36
【问题描述】:

我正在尝试使用以下代码定位搜索栏并将输入文本发送给它:

searchBar = browser.find_element_by_xpath('//*[@id="u_e_2"]/input[2]')

searchBar.send_keys("my-text-here")
browser.implicitly_wait(10)
searchBar.send_keys(Keys.ENTER) 

但出现以下错误:

File ".\get-quotes.py", line 46, in <module>
    searchBar = browser.find_element_by_xpath('//*[@id="u_e_2"]/input[2]')
  File "D:\ahmed-quotes\quote-env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "D:\ahmed-quotes\quote-env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "D:\ahmed-quotes\quote-env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "D:\ahmed-quotes\quote-env\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="u_e_2"]/input[2]"}
  (Session info: chrome=81.0.4044.138)

知道我在 Chrome 浏览器中检查元素时复制了 xpath。
我的代码有什么问题?

【问题讨论】:

  • 请发布 HTML 或网站 URL

标签: python selenium google-chrome


【解决方案1】:

Facebook 对其 HTML 进行了大规模混淆,以防止您做这种事情。这包括为每页生成随机 ID、标签和其他属性。

【讨论】:

    猜你喜欢
    • 2023-01-21
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 2020-06-14
    • 2021-01-02
    • 1970-01-01
    相关资源
    最近更新 更多