Virginia Walters
Virginia Walters

Reputation: 23

Selenium Exception NoSuchElementException: Unable to locate element: {"method":"xpath","selector":

When running this code after making changes form find_element_by_xpath to find_element(By.XPATH I am now encountering an error of not be able to locate the element.

        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)

Upvotes: 1

Views: 1410

Answers (1)

catch32
catch32

Reputation: 18582

The exception message is quite clear:

NoSuchElementException

Have you tried your XPATH - if it gives exactly the right element?

For now it looks like:

//*[@id='viewContainer']/app-listing-container/app-search-container/app-search-form-container/form/div[3]/button[2]

Much better will be depicted for which exactly element has such id, like:

//button[@id='id-name']

And try to omit

div[3]/button[2]

if more elements (like more buttons) will be added your XPATH fails.

Also, it could be that this element hasn't been loaded yet. You could add explicit wait.

UPDATE:

For processing Xpath locators from page you able to use extension for Firefox browser:

You could use the browser's console for it as well.

Upvotes: 1

Related Questions