I have a problem with scraping amazon logistics, I go to the website, enter my credentials successfully, pick the date I want to show data for, click it.
the problem StaleElementReferenceException arises when I go through links to perform:
1.search in the search bar for the user ID
2.Once the user is there, click it.
3.scrape information from the page i am currently at.
4.click on the back button
5.click on the "Clear field" button.
6.repeat.
the problem happens after the 6th step when I try to locate the the search bar!
it shows:
StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: MicrosoftEdge=105.0.1343.33)
HTML Source :
<div class="css-14lg5yy"><div class="css-f3b2gg"><input aria-describedby="search-field-709-suffix" class="css-1sq0sbt" id="search-field-709" type="text" placeholder="Filter by name or ID transporter, scan, order or route" autocomplete="off" spellcheck="false" inputmode="text" value=""></div></div>
My Code:
for i in IDs:
#loop over Each ID Extracted
current_id = i.text
#Search for the current ID
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'[class="css-1sq0sbt"]'))).send_keys(current_id)
time.sleep(2)
#clicking on the person viewd.
driver.find_element(By.CSS_SELECTOR,'[class="af-link link-to-content-selectable "]').click()
time.sleep(2)
#Scraping Stuff
name = driver.find_element(By.CSS_SELECTOR,'[class="header-transporter-name font-weight-bold pr-2"]').text
#Clicking on the back button
driver.find_element(By.CSS_SELECTOR,'[class="fa fa-arrow-left RTL-mirror-vertically"]').click()
time.sleep(2)
#Clearing The Search
driver.find_element(By.CSS_SELECTOR,'[aria-label="Clear search"]').click()
time.sleep(1.5)
print(name)
The class="css-1sq0sbt" only appears after we hit the "clear" button, so, I don't think it's a problem.