1

I am new to python and still learning.

I am just playing around with small scripts I am creating for fun to learn, here is what I got.

from selenium import webdriver
import time
import random

driver = webdriver.Chrome("C:\\Users\\16239\\youtubebot\\chromedriver")


videos = [
    "https://www.youtube.com/watch?v=COAdbTDTDFc"
    "https://www.youtube.com/watch?v=p_2wemVUQnc"
    "https://www.youtube.com/watch?v=A8fODOFiBq0"
    "https://www.youtube.com/watch?v=tmgT01e1sGQ"
    "https://www.youtube.com/watch?v=_eqEuBp3jJw"
    "https://www.youtube.com/watch?v=09IRNL2TsHM"
    "https://www.youtube.com/watch?v=uUIugt-GsbY"
    "https://www.youtube.com/watch?v=HqlBwWX218s"
    "https://www.youtube.com/watch?v=DrKafd3U4TY"
]

random_video = random.randint(0,8)
sleep_time = random.randint(60,120)

for i in range(7000):
    print("Running the Video for {} time".format(i))
    driver.get(videos[random_video])
    time.sleep(sleep_time)

    driver.quit()

the issue is when I run it I am getting the error.

driver.get(videos[random_video])
IndexError: list index out of range

I have 9 in the videos list and the random_video variable is calling 0 - 8 and I cant figure out the error. please help

2 Answers2

2

There is no comma , at the end of video URL in your videos list.

videos = [
    "https://www.youtube.com/watch?v=COAdbTDTDFc",
    "https://www.youtube.com/watch?v=p_2wemVUQnc",
    "https://www.youtube.com/watch?v=A8fODOFiBq0",
    "https://www.youtube.com/watch?v=tmgT01e1sGQ",
    "https://www.youtube.com/watch?v=_eqEuBp3jJw",
    "https://www.youtube.com/watch?v=09IRNL2TsHM",
    "https://www.youtube.com/watch?v=uUIugt-GsbY",
    "https://www.youtube.com/watch?v=HqlBwWX218s",
    "https://www.youtube.com/watch?v=DrKafd3U4TY"
]
Ram
  • 4,724
  • 2
  • 14
  • 22
0

you did not separate the videos with , python thinks it only has 1 element

videos = [
    "https://www.youtube.com/watch?v=COAdbTDTDFc",
    "https://www.youtube.com/watch?v=p_2wemVUQnc",
    "https://www.youtube.com/watch?v=A8fODOFiBq0",
    "https://www.youtube.com/watch?v=tmgT01e1sGQ",
    "https://www.youtube.com/watch?v=_eqEuBp3jJw",
    "https://www.youtube.com/watch?v=09IRNL2TsHM",
    "https://www.youtube.com/watch?v=uUIugt-GsbY",
    "https://www.youtube.com/watch?v=HqlBwWX218s",
    "https://www.youtube.com/watch?v=DrKafd3U4TY"
]

by the way, randint is (inclusive, exclusive) so the last video will never be picked AND you format the number of iteration not the time like you are looking to do.

Dr. Prof. Patrick
  • 1,280
  • 2
  • 15
  • 27