-1

I need to find search box and then need to enter the ID and search for it.

I have tried below code but getting error.

Search_Box= driver.find_element_by_xpath('//*[@id="search-field-76125600690"]')
Search_Box.send_keys('12345')

HTML Code for Search Box:

<input type="search" id="search-field-38582485921" placeholder="Search by Name or CRD#" maxlength="30" aria-label="Search by Name or CRD#">
  • It says it can't find an element with that id - are you sure it exists on whatever page you're trying to scrape? If you're not guaranteed to have this on any particular page, you'll need to catch the exception. – wkl Aug 19 '22 at 15:15
  • Yes, Id is same. – Arvind Kumar Yadav Aug 19 '22 at 15:16
  • You're going to have to double check that because the exception is telling you it can't find it. – wkl Aug 19 '22 at 15:18
  • Check I have added the picture in Question. – Arvind Kumar Yadav Aug 19 '22 at 15:24
  • Double check your image. The `id` you're searching for in code is different from the one in your image. Your error has the same id as your image but it's different from your code. – wkl Aug 19 '22 at 15:25
  • I have made that change but still getting sam error. – Arvind Kumar Yadav Aug 19 '22 at 15:28
  • you didn't show URL for this page so we can't check how it behaves - maybe this element is in ` – furas Aug 19 '22 at 22:42

2 Answers2

0

Automation/Scrapping debugging tip:

  1. Go to the webpage you are scraping in a browser.
  2. Open the devtool console (docs).
  3. Since you are looking for an HTML element with xpath //*[@id="search-field-270852010"], enter $x('//*[@id="search-field-270852010"]') and press enter.
  4. This will execute a jquery search for the specified element.
  5. If nothing or an empty array is returned, then the xpath is incorrect.

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
Sol Zilberman
  • 16
  • 1
  • 1
  • How I can resolve this issue. I thing this is issue with the element is hidden/not loaded on the page at the time the script is checking – Arvind Kumar Yadav Aug 19 '22 at 16:40
  • Check out this documentation - it should allow you to wait until an element is visible before returning: [wait in Selenium](https://selenium-python.readthedocs.io/waits.html). You can also try to use either the **full xpath** or another method of locating other than XPATH (bycss, byclassname, byid). – Sol Zilberman Aug 19 '22 at 16:49
0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352