Selenium 异常 NoSuchElementException:无法定位元素:{"method":"xpath","selector":
Selenium Exception NoSuchElementException: Unable to locate element: {"method":"xpath","selector":
当 运行 将此代码从 find_element_by_xpath 更改为 find_element 时(By.XPATH 我现在遇到无法定位元素的错误。
search_btn = self.driver.find_element(By.XPATH, "//*[@id='viewContainer']/app-listing-container/app-search-container/app-search-form-container/form/div[3]/button[2]")
time.sleep(4)
search_btn.click()
time.sleep(5)
'''
DevTools listening on ws://127.0.0.1:63724/devtools/browser/c4f952a3-afd0-4074-8aaf-452394b3300d
[22840:23968:0212/182455.570:ERROR:chrome_browser_main_extra_parts_metrics.cc(227)] START: ReportBluetoothAvailability(). If you don't see the END: message, this is crbug.com/1216328.
[22840:23968:0212/182455.570:ERROR:chrome_browser_main_extra_parts_metrics.cc(230)] END: ReportBluetoothAvailability()
[22840:23968:0212/182455.571:ERROR:chrome_browser_main_extra_parts_metrics.cc(235)] START: GetDefaultBrowser(). If you don't see the END: message, this is crbug.com/1216328.
[22840:19460:0212/182455.579:ERROR:device_event_log_impl.cc(214)] [18:24:55.579] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[22840:19460:0212/182455.587:ERROR:device_event_log_impl.cc(214)] [18:24:55.587] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[22840:23968:0212/182455.590:ERROR:chrome_browser_main_extra_parts_metrics.cc(239)] END: GetDefaultBrowser()
Traceback (most recent call last):
File "C:\!!!Angela\Paid screen scraper\data scraping\data scraping\my_ui_scrapy12.py", line 277, in show_page
search_btn = self.driver.find_element(
File "C:\Users\vwalt_000\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1244, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\vwalt_000\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "C:\Users\vwalt_000\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, 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='viewContainer']/app-listing-container/app-search-container/app-search-form-container/form/div[3]/button[2]"}
(Session info: chrome=98.0.4758.82)
异常信息很清楚:
NoSuchElementException
您是否尝试过您的 XPATH - 如果它给出了正确的元素?
现在看起来像:
//*[@id='viewContainer']/app-listing-container/app-search-container/app-search-form-container/form/div[3]/button[2]
更好的描述是哪个元素有这样的id
,比如:
//button[@id='id-name']
并尽量省略
div[3]/button[2]
如果要添加更多元素(如更多按钮),您的 XPATH 将失败。
另外,可能是这个元素还没有加载。您可以添加 explicit wait.
更新:
用于处理来自页面的 Xpath 定位器,您可以使用 Firefox 浏览器的扩展程序:
您也可以使用浏览器的控制台。
当 运行 将此代码从 find_element_by_xpath 更改为 find_element 时(By.XPATH 我现在遇到无法定位元素的错误。
search_btn = self.driver.find_element(By.XPATH, "//*[@id='viewContainer']/app-listing-container/app-search-container/app-search-form-container/form/div[3]/button[2]")
time.sleep(4)
search_btn.click()
time.sleep(5)
'''
DevTools listening on ws://127.0.0.1:63724/devtools/browser/c4f952a3-afd0-4074-8aaf-452394b3300d
[22840:23968:0212/182455.570:ERROR:chrome_browser_main_extra_parts_metrics.cc(227)] START: ReportBluetoothAvailability(). If you don't see the END: message, this is crbug.com/1216328.
[22840:23968:0212/182455.570:ERROR:chrome_browser_main_extra_parts_metrics.cc(230)] END: ReportBluetoothAvailability()
[22840:23968:0212/182455.571:ERROR:chrome_browser_main_extra_parts_metrics.cc(235)] START: GetDefaultBrowser(). If you don't see the END: message, this is crbug.com/1216328.
[22840:19460:0212/182455.579:ERROR:device_event_log_impl.cc(214)] [18:24:55.579] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[22840:19460:0212/182455.587:ERROR:device_event_log_impl.cc(214)] [18:24:55.587] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[22840:23968:0212/182455.590:ERROR:chrome_browser_main_extra_parts_metrics.cc(239)] END: GetDefaultBrowser()
Traceback (most recent call last):
File "C:\!!!Angela\Paid screen scraper\data scraping\data scraping\my_ui_scrapy12.py", line 277, in show_page
search_btn = self.driver.find_element(
File "C:\Users\vwalt_000\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1244, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\vwalt_000\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "C:\Users\vwalt_000\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, 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='viewContainer']/app-listing-container/app-search-container/app-search-form-container/form/div[3]/button[2]"}
(Session info: chrome=98.0.4758.82)
异常信息很清楚:
NoSuchElementException
您是否尝试过您的 XPATH - 如果它给出了正确的元素?
现在看起来像:
//*[@id='viewContainer']/app-listing-container/app-search-container/app-search-form-container/form/div[3]/button[2]
更好的描述是哪个元素有这样的id
,比如:
//button[@id='id-name']
并尽量省略
div[3]/button[2]
如果要添加更多元素(如更多按钮),您的 XPATH 将失败。
另外,可能是这个元素还没有加载。您可以添加 explicit wait.
更新:
用于处理来自页面的 Xpath 定位器,您可以使用 Firefox 浏览器的扩展程序:
您也可以使用浏览器的控制台。