pythonseleniumwebdriver

Selenium webdriver using Python to navigate to a certain point in a video on youtube


I have a requirement to navigate to a certain time point in a Youtube video using Selenium webdriver with Python. I have the following code which will start from the beginning of the Youtube video and my requirement is to be able to start from a specific point like , 07 mins : 45 seconds when I run the Python program. Any pointers on how I could achieve this. As of now, I have the following code which is working to open the video and start from the beginning.

#importing webdriver from selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(executable_path = 'C:/bit9prog/dev/chromedriver_win32/chromedriver')
url = "https://www.youtube.com/watch?v=5ygpvZbxA6w"

driver.get(url)
# Obtain the length of the youtube video
# wait for the page to load everything (works without it)
for i in range(2):
    print(i)
    time.sleep(1)
video = driver.find_element(By.ID,'movie_player')
video.send_keys("k") #hits space
time.sleep(1)

Solution

  • add time to the url like so "https://youtu.be/5ygpvZbxA6w?t=465" the video will start at 465 which is 7 min 45 sec