Automate Youtube with Python Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we are going to see how to automate Youtube using Python. Module usedSelenium: It controlling a web browser through the programpyttsx3: It is a text-to-speech conversion library in Pythonspeech_recognition: Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etcpyaudio: It is used to play and record audio on a variety of platforms Make sure that you have noted the location where the chromedriver has been downloaded (as it is used in our python script). Now After downloading extract the zip file and please note the file location of the extracted file as we have needed it later in python code. (You can find the location by clicking on properties and then details). ApproachImport the library which we have installed.Then we have to take the input search query using the speech_recognition library. we can do this by making an instance of speech_recognition library as sr.Recognizer() .After this adjusting the threshold frequency and convert the voice input into a string.Then, the main part comes into the picture, we have created a function automateYoutube() which plays the required video from Youtube.In this function create we driver instance using function webdriver.Chrome() which takes the path of chromedriver as the parameter.Finally, find the name or id or class or CSS selector of the search bar and search button by right-clicking inspect search bar and .search button.Now, just call the automateYoutube() function to see the output.Now we have to automate play/pause buttons for that we again find the CSS selector of play/pause button using selenium.Below is the Full Implementation Python3 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 from selenium.webdriver.common.keys import Keys import speech_recognition as sr import pyttsx3 import time def automateYoutube(searchtext): # giving the path of chromedriver to selenium webdriver path = "C:\\Users\\hp\\Downloads\\chromedriver" url = "https://www.youtube.com/" # opening the youtube in chromedriver driver = webdriver.Chrome(path) driver.get(url) # find the search bar using selenium find_element function driver.find_element_by_name("search_query").send_keys(searchtext) # clicking on the search button driver.find_element_by_css_selector( "#search-icon-legacy.ytd-searchbox").click() # For finding the right match search WebDriverWait(driver, 0).until(expected_conditions.title_contains(MyText)) # clicking on the match search having same as in searched query WebDriverWait(driver, 30).until( expected_conditions.element_to_be_clickable((By.ID, "img"))).click() # while(True): # pass speak = sr.Recognizer() try: with sr.Microphone() as speaky: # adjust the energy threshold based on # the surrounding noise level speak.adjust_for_ambient_noise(speaky, duration=0.2) print("listening...") # listens for the user's input searchquery = speak.listen(speaky) # Using google to recognize audio MyText = speak.recognize_google(searchquery) MyText = MyText.lower() except sr.RequestError as e: print("Could not request results; {0}".format(e)) except sr.UnknownValueError: print("unknown error occurred") # Calling the function automateYoutube(MyText) Output: Comment More infoAdvertise with us B bansalshubhamcse21 Follow Improve Article Tags : Python Blogathon-2021 Python-selenium Python Selenium-Exercises Practice Tags : python Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 6 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 6 min read Loops in Python - For, While and Nested Loops 9 min read Python Functions 9 min read Recursion in Python 6 min read Python Lambda Functions 6 min read Python Data StructuresPython String 6 min read Python Lists 6 min read Python Tuples 6 min read Dictionaries in Python 7 min read Python Sets 10 min read Python Arrays 9 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 6 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 11 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 10 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like