Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

Python Selenium waits. Preventing Stale errors when pages are identical

I am tying to scrape a website that loads the identical URL but with an updated store and prices. I am getting stale errors on zipStore1.click() about half of the time. I am also looking for a better solution to time.sleep(10) in-between page loads. I have gone through many possible solutions and none seem to work. Finally, am I using the WebDriverWait methods correctly?

for zip in zipCodes:

# opening the webDriver
driver = webdriver.Chrome('chromedriver.exe')
driver.implicitly_wait(100)
driver.maximize_window()

#Open product URL
driver.get("https://www.homedepot.com"
           "/p/Everbilt-1-2-in-x-4-ft-x-25-ft-19-Gauge-Steel-Hardware-Cloth-308226EB/205960849")
print("Opened url, setting zip")

# set the zip code
yourStore = WebDriverWait(driver, 10, ignored_exceptions=ignored_exceptions).until(
    EC.element_to_be_clickable((By.XPATH, "//*[@id='myStore']/a/span")))
yourStore.click()

find_Other_Stores = WebDriverWait(driver, 10, ignored_exceptions=ignored_exceptions).until(
    EC.element_to_be_clickable((By.XPATH, "//*[@id='myStoreDropdown']/div/div[4]/a/span")))
find_Other_Stores.click()

zipInput = WebDriverWait(driver, 10, ignored_exceptions=ignored_exceptions).until(
    EC.element_to_be_clickable((By.XPATH, "//*[@id='myStore-formInput']")))
zipInput.send_keys(zip)
zipInput.send_keys(Keys.RETURN)

zipStore1 = WebDriverWait(driver, 10, ignored_exceptions=ignored_exceptions).until(
    EC.element_to_be_clickable((By.XPATH, "//*[@id='myStore-list']/div[1]/div[6]/button/span[2]")))
zipStore1.click()


#this is a temp solution
time.sleep(10)


# select the WebElement that contains the price, and print it
results_D = driver.find_element_by_xpath("//*[@id='standard-price']/div/div/span[2]")
dollars = results_D.get_attribute("innerHTML")
print(dollars)

results_C = driver.find_element_by_xpath("//*[@id='standard-price']/div/div/span[3]")
cents = results_C.get_attribute("innerHTML")
print(cents)
total = int(dollars) + (float(cents) / 100)
print(total)

# Close out the webDriver
driver.close()

Edit 1: The error is: StaleElementReferenceException

question from:https://stackoverflow.com/questions/65830207/python-selenium-waits-preventing-stale-errors-when-pages-are-identical

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...