StaleElementException when iterating with Python

前端 未结 2 1831
粉色の甜心
粉色の甜心 2020-11-22 05:00

I\'m trying to create a basic web scraper for Amazon results. As I\'m iterating through results, I sometimes get to page 5 (sometimes only page 2) of the results and then a

2条回答
  •  甜味超标
    2020-11-22 06:00

    If you just want your script to iterate over all the result pages, you don't need any complicated logic - just make a click on Next button while it's possible:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait as wait
    from selenium.common.exceptions import TimeoutException
    
    driver = webdriver.Chrome()
    
    driver.get('https://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=sonicare+toothbrush')
    
    while True:
        try:
            wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a > span#pagnNextString'))).click()
        except TimeoutException:
            break
    

    P.S. Also note that implicitly_wait(10) should not wait full 10 seconds, but wait up to 10 seconds for element to appear in HTML DOM. So if element is found within 1 or 2 seconds then wait is done and you will not wait rest 8-9 seconds...

提交回复
热议问题