Search code examples
pythonseleniumtimeout

Timeout error using selenium in Python, although the code runs fine


I'm new here and new to Python too! I have a huge movie archive on my disk, I wrote a code to download movie posters, using movie names on my disk and put the poster inside each movie folder.

from os import listdir
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
import requests

# Loading Film Names
film_names = [f for f in listdir("I:\\Movies")]

# Loading Image Names
image_names = [f for f in listdir("I:\\Movies")]

# Opening Chrome
driver = webdriver.Chrome()
driver.get('http://imdb.com')

counter = 0

for i in film_names[counter:]:
    # filtering movie names to become without director names and no release date
    parantez_num = i.find('(')
    film_names[counter] = i[0:parantez_num - 1]

    print(counter)

    # Typing Film Name in Search Box
    searchbox = driver.find_element_by_xpath('//*[@id="suggestion-search"]')
    searchbox.send_keys(film_names[counter])

    # Clicking Search Button
    searchbutton = driver.find_element_by_xpath('//*[@id="suggestion-search-button"]')
    searchbutton.click()

    # Clicking First Result
    firstsearch = WebDriverWait(driver, 5).until(
    expected_conditions.element_to_be_clickable((By.XPATH, '//* 
    [@id="main"]/div/div[2]/table/tbody/tr[1]/td[2]/a'))
    )
    firstsearch.click()

    # Maximizing Image
    wait = WebDriverWait(driver, 10)
    image_magnify = wait.until(expected_conditions.element_to_be_clickable((By.XPATH, '//* 
    [@id="title-overview-widget'''

    '"]/div[1]/div[3]/div[1]/a/img')))
    image_magnify.click()

    # Saving The Image
    image = WebDriverWait(driver, 5).until(
    expected_conditions.element_to_be_clickable((By.XPATH, '//*[@id="photo- 
    container"]/div/div[3]/div/div[2]/div['
                                                           '1]/div[2]/div/img[2]'))
    )
    image_url = (image.get_attribute('src'))
    with open("I:\\Movies\\" + image_names[counter] + "\\" + image_names[counter] + ".jpg", "wb") as 
    f:
    f.write(requests.get(image_url).content)

    # going back to the first page of IMDB
    driver.back()
    driver.back()
    driver.back()

    counter += 1

It works fine. But after a random number of successful downloads, suddenly it stops and gives an error:

Traceback (most recent call last):
File "C:/Users/Kurdman/Desktop/Poster Downloader/Main/MainCode.py", line 42, in <module>
image_magnifier = wait.until(expected_conditions.element_to_be_clickable((By.XPATH, '//*[@id="title- 
overview-widget'
File "C:\Users\Kurdman\Desktop\Poster Downloader\Main\venv\lib\site- 
packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

I wonder why I receive this error, because the "for loop" works fine for like 10 movies, then suddenly it stops. Even if I set the counter to something like 100, to start from the 100th movie, it works for some of them and then stops again and gives this error.

By the way, my movie names are in such format: Ajami (2009) - Scandar Copti - Foreign Language Oscar Nominee so I filter the names to remove the release date and director names and other things, by finding the index of the first parenthesis in the title.


Solution

  • Yes there are chances of getting TimeOutError when the element takes more than expected time to load you can also handle this error by just slightly changing your for loop. try using the code given below for i in film_names[counter:]:

    try:
        parantez_num = i.find('(')
        film_names[counter] = i[0:parantez_num - 1]
    
        print(counter)
    
        # Typing Film Name in Search Box
        searchbox = driver.find_element_by_xpath('//*[@id="suggestion-search"]')
        searchbox.send_keys(film_names[counter])
    
        # Clicking Search Button
        searchbutton = driver.find_element_by_xpath('//*[@id="suggestion-search-button"]')
        searchbutton.click()
    
        # Clicking First Result
        firstsearch = WebDriverWait(driver, 5).until(
            expected_conditions.element_to_be_clickable((By.XPATH, '//*
            [ @ id="main"] / div / div[2] / table / tbody / tr[1] / td[2] / a'))
                                                         )
        firstsearch.click()
    
        # Maximizing Image
        wait = WebDriverWait(driver, 10)
        image_magnify = wait.until(expected_conditions.element_to_be_clickable((By.XPATH, '//*
        [ @ id="title-overview-widget'''
    
                                                                                '"]/div[1]/div[3]/div[1]/a/img')))
        image_magnify.click()
    
        # Saving The Image
        image = WebDriverWait(driver, 5).until(
            expected_conditions.element_to_be_clickable((By.XPATH, '//*[@id="photo-
                                                         container"]/div/div[3]/div/div[2]/div['
                                                         '1]/div[2]/div/img[2]'))
        )
        image_url = (image.get_attribute('src'))
        with open("I:\\Movies\\" + image_names[counter] + "\\" + image_names[counter] + ".jpg", "wb") as
        f:
            f.write(requests.get(image_url).content)
    
        # going back to the first page of IMDB
        driver.back()
        driver.back()
        driver.back()
    
        counter += 1
    except TimeoutError:
        counter += 0
    

    just try editing the for loop with this will handle that error if it appears again