Home > Blockchain >  NoSuchElementException: Message: no such element: Unable to locate element in Python
NoSuchElementException: Message: no such element: Unable to locate element in Python

Time:08-20

I am trying to do web automation using Python and getting attached error. Can anyone help me with this.

user_id =  driver.find_element_by_xpath('//*[@id="search-field-270852010"]')
user_id_textbox.send_keys('123456')
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="search-field-27085201070"]"}
  (Session info: chrome=104.0.5112.101)
  (Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 10.0.19044 x86_64)

Let me know if more information is required.

CodePudding user response:

Automation/Scrapping debugging tip:

  1. Go to the webpage you are scraping in a browser.
  2. Open the devtool console (ex

    In general, this is not an issue with your coding logic but rather the web driver cannot locate the HTML element with the given xpath which means either:

    • the xpath is wrong
    • the element is hidden/not loaded on the page at the time the script is checking

    CodePudding user response:

    The last part of the value of the id attribute i.e. 270852010 is dynamically generated and is bound to change sooner/later. They may change next time you access the application afresh or even while next application startup. So can't be used in locators.


    Solution

    To locate the element you can use either of the following locator strategies:

    • Using css_selector:

      user_id = driver.find_element(By.CSS_SELECTOR, "[id^='search-field']")
      
    • Using xpath:

      user_id = driver.find_element(By.XPATH, "//*[starts-with(@id, 'search-field')]")
      
    • Note : You have to add the following imports :

      from selenium.webdriver.common.by import By
      
  • Related