Skip to content

blissfulProgrammer/selenium-youtube

Repository files navigation

YouTube Selenium Automation Scripts

This repository contains Selenium scripts that automate browsing YouTube to search for "computer donation program" and play a video from the "Kids Longhorn" channel.

πŸ“ Files

  • youtube_selenium_script.py - Non-headless version (opens visible browser window)
  • youtube_selenium_script_headless.py - Headless version (runs in background without GUI)

πŸš€ Features

  • Automatically navigates to YouTube
  • Searches for "computer donation program"
  • Identifies videos from the "Kids Longhorn" channel (@kidslonghorn3971)
  • Plays the found video automatically
  • Robust error handling and multiple selector strategies
  • Cross-platform compatibility (Windows & Linux)

πŸ“‹ Prerequisites

For All Operating Systems

  1. Python 3.7 or higher
  2. Google Chrome browser (latest version recommended)
  3. ChromeDriver (matching your Chrome version)

πŸ› οΈ Installation Instructions

Windows Setup

Step 1: Install Python

  1. Download Python from python.org
  2. During installation, check "Add Python to PATH"
  3. Verify installation:
    python --version
    pip --version

Step 2: Install Google Chrome

  1. Download from chrome.google.com
  2. Install with default settings
  3. Note your Chrome version: Chrome Menu > Help > About Google Chrome

Step 3: Install ChromeDriver

Option A: Automatic (Recommended)

pip install webdriver-manager

Option B: Manual

  1. Download ChromeDriver from chromedriver.chromium.org
  2. Extract to a folder (e.g., C:\chromedriver\)
  3. Add the folder to your system PATH:
    • Right-click "This PC" β†’ Properties β†’ Advanced System Settings
    • Click "Environment Variables"
    • Under "System Variables", find "Path" and click "Edit"
    • Click "New" and add your ChromeDriver folder path
    • Click "OK" to save

Step 4: Install Python Dependencies

pip install selenium webdriver-manager

Step 5: Clone and Run

git clone https://github.com/blissfulProgrammer/selenium-youtube.git
cd selenium-youtube
python youtube_selenium_script.py

Linux Setup (Ubuntu/Debian)

Step 1: Update System

sudo apt update && sudo apt upgrade -y

Step 2: Install Python and pip

sudo apt install python3 python3-pip -y
python3 --version
pip3 --version

Step 3: Install Google Chrome

# Download and install Chrome
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" | sudo tee /etc/apt/sources.list.d/google-chrome.list
sudo apt update
sudo apt install google-chrome-stable -y

# Verify installation
google-chrome --version

Step 4: Install ChromeDriver

Option A: Automatic (Recommended)

pip3 install webdriver-manager

Option B: Manual

# Find your Chrome version first
CHROME_VERSION=$(google-chrome --version | grep -oP '\d+\.\d+\.\d+')
echo "Chrome version: $CHROME_VERSION"

# Download matching ChromeDriver
wget -O chromedriver.zip "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${CHROME_VERSION%.*}/chromedriver_linux64.zip"
unzip chromedriver.zip
sudo mv chromedriver /usr/local/bin/
sudo chmod +x /usr/local/bin/chromedriver
rm chromedriver.zip

# Verify installation
chromedriver --version

Step 5: Install Python Dependencies

pip3 install selenium webdriver-manager

Step 6: Clone and Run

git clone https://github.com/blissfulProgrammer/selenium-youtube.git
cd selenium-youtube
python3 youtube_selenium_script.py

Linux Setup (CentOS/RHEL/Fedora)

Step 1: Install Python and pip

# For CentOS/RHEL
sudo yum install python3 python3-pip -y

# For Fedora
sudo dnf install python3 python3-pip -y

Step 2: Install Google Chrome

# Add Google Chrome repository
sudo tee /etc/yum.repos.d/google-chrome.repo <<EOF
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64
enabled=1
gpgcheck=1
gpgkey=https://dl.google.com/linux/linux_signing_key.pub
EOF

# Install Chrome
sudo yum install google-chrome-stable -y
# OR for Fedora: sudo dnf install google-chrome-stable -y

Step 3: Follow steps 4-6 from Ubuntu/Debian setup above

🎯 Usage

Running the Scripts

Non-Headless Version (Visible Browser)

# Windows
python youtube_selenium_script.py

# Linux
python3 youtube_selenium_script.py

Headless Version (Background)

# Windows
python youtube_selenium_script_headless.py

# Linux
python3 youtube_selenium_script_headless.py

Expected Output

Navigating to YouTube...
Looking for search box...
Searching for 'computer donation program'...
Looking for videos from 'Kids Longhorn' channel...
Found 20 videos using selector: div#contents ytd-video-renderer
Checking if 'Kids Longhorn' exists in page source...
Found 'Kids Longhorn' in page source!
Found video from Kids Longhorn channel!
Video title: Computer Donation Program
Waiting for video to load...
Looking for play button...
Script completed successfully!
Video from Kids Longhorn channel should now be playing.

πŸ”§ Troubleshooting

Common Issues

"ChromeDriver not found" Error

Solution: Ensure ChromeDriver is installed and in your PATH, or use webdriver-manager:

from webdriver_manager.chrome import ChromeDriverManager
service = Service(ChromeDriverManager().install())

"Chrome binary not found" Error

Solution: Install Google Chrome or specify the Chrome binary path:

chrome_options.binary_location = "/path/to/chrome"  # Linux
chrome_options.binary_location = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"  # Windows

Version Mismatch Error

Solution: Update both Chrome and ChromeDriver to matching versions:

# Check Chrome version
google-chrome --version  # Linux
# OR check in Chrome: Menu > Help > About Google Chrome

# Download matching ChromeDriver from chromedriver.chromium.org

Permission Denied (Linux)

Solution: Make ChromeDriver executable:

sudo chmod +x /usr/local/bin/chromedriver

Display Issues (Linux Headless Systems)

Solution: Install virtual display:

sudo apt install xvfb -y
xvfb-run -a python3 youtube_selenium_script.py

Script-Specific Issues

Video Not Found

  • The script searches for videos from "Kids Longhorn" channel (@kidslonghorn3971)
  • If the video is not found, check if the channel has videos matching "computer donation program"
  • The script will show available channels in search results for debugging

Slow Loading

  • Increase wait times in the script if your internet connection is slow
  • Modify time.sleep() values as needed

πŸ”’ Security Notes

  • These scripts are for educational purposes
  • Respect YouTube's Terms of Service
  • Don't use for automated spam or abuse
  • Consider rate limiting for production use

πŸ“ Customization

Changing Search Terms

Modify the search query in the script:

search_box.send_keys("your search term here")

Changing Target Channel

Update the channel detection logic:

has_kids_longhorn = ("Your Channel Name" in channel_name or 
                   "yourchannel" in video_html.lower() or 
                   "@yourchannelhandle" in video_html)

Adding Wait Times

Adjust timing for slower connections:

time.sleep(5)  # Increase from 3 to 5 seconds

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test on both Windows and Linux
  5. Submit a pull request

πŸ“„ License

This project is open source and available under the MIT License.

πŸ†˜ Support

If you encounter issues:

  1. Check the troubleshooting section above
  2. Verify your Chrome and ChromeDriver versions match
  3. Ensure all dependencies are installed correctly
  4. Check your internet connection
  5. Review the console output for specific error messages

For additional help, please open an issue in the GitHub repository with:

  • Your operating system and version
  • Python version
  • Chrome version
  • ChromeDriver version
  • Complete error message
  • Steps to reproduce the issue

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •