无法使用XPATH在Facebook中找到搜索栏元素

2024-09-28 23:18:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试查找搜索栏并使用以下代码向其发送输入文本:

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的。
我的代码有什么问题


Tags: inpyenvbyseleniumlineelementfind