DEV Community

Cover image for Selenium for Beginners: Automating a Search on YouTube

Posted on • Updated on

Selenium for Beginners: Automating a Search on YouTube

Selenium is a popular open-source tool that automates web browsers and is commonly used for testing web applications. In this article, we will use the Firefox driver to navigate to the YouTube home page, locate the search bar, and enter a search query.


  • Python 3.10
  • Selenium library
  • Firefox browser


Install the Selenium library using pip:

pip install selenium
Enter fullscreen mode Exit fullscreen mode


  1. Import the necessary libraries

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from import By
  2. Create a new instance of the Firefox driver

    driver = webdriver.Firefox()
  3. Navigate to the YouTube home page

  4. Locate the search bar using xpath '//*[@id="search"]'

    search_bar = driver.find_element(By.XPATH, '//*[@id="search"]')
  5. Enter a search query

  6. Press the Enter key to submit the search query

  7. (Optional) Close the browser


By following these steps, you can automate a search on the YouTube website using the Selenium Python module. You can modify the code to search for anything you like, and it can be used as a foundation for more complex automation tasks. The full code can be found here.

Latest comments (0)