Browser Crashes in Page Retrieving Loop, Python Selenium Script
Question: I wrote this simple script - it simply fetches an image from blogposts and
posts it to Pinterest. It works great, except that after about 43 pages, the
browser hangs/freezes.

I'm wondering if there is some sort of "leak" causing things to get out of
control? Perhaps there is something I can alter in the script to make it run
without crashing?

Here is my code:

    
    
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import pickle
    import os.path
    
    import time
    driver = webdriver.Firefox()
    
    #START THE RACE
    
    driver.get("https://www.pinterest.com/login/")
    assert "Pinterest" in driver.title
    
    #name
    elem = driver.find_element_by_name("username_or_email")
    elem.send_keys("someone@something.com")
    
    #pass
    elem = driver.find_element_by_name("password")
    elem.send_keys("12345")
    
    elem.send_keys(Keys.RETURN)
    
    time.sleep(5)
    
    new_url = 'something.com/something_else'
    driver.get(new_url)
    i=0
    
    while(1):
        i=i+1
        print i
    
        time.sleep(5)
    
        driver.find_element_by_css_selector(".pin-it-btn-wrapper a").click();
    
        time.sleep(3)
    
        try:
            driver.find_element_by_css_selector('[data-pin-index="0"]').click();
        except:
            driver.find_element_by_css_selector("#prev_post a").click();
    
        time.sleep(3)
    
        handles = driver.window_handles
    
        if(handles):
            for handle in handles:
                driver.switch_to_window(handle)
                try:
                    driver.find_element_by_css_selector('button.pinIt').click();                
                    time.sleep(2)
    
                except:
                    continue
    
        time.sleep(3) 
    
        if(handles):
            for handle in handles:
                driver.switch_to_window(handle)
                try:
                    time.sleep(1)    
                    driver.find_element_by_css_selector("#prev_post a").click();
                    time.sleep(3)
                    break
                except:
                    continue   
    
    
    #assert "No results found." not in driver.page_source
    #driver.close()
    

The script itself logs into pinterest, goes to given site, and starts crawling
the "previous post" links of the blog posts - pushes the "Pin it" button,
selects the image, confirms (separate window) then jumps back to main window
and starts another page.

As mentioned, I think its possible there is a "leak" somewhere causing it to
grind to a hault after a while.


Answer: Per commenter's advice, simply changing every instance of:

    
    
    time.sleep(3)
    

to

    
    
    driver.implicitly_wait(3)
    

seems to have helped things. Also I updated to most recent version.

