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#">
CodePudding user response:
Automation/Scrapping debugging tip:
- Go to the webpage you are scraping in a browser.
- Open the devtool console (
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