【发布时间】: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