Automatically presses the 'skip' button on yt videos to skip ads.
This is a very clean and robust script for automating a common web interaction task. It's essentially a dedicated YouTube Ad Skipper built using Selenium.
Here is a proper README.md for your script:
This Python script provides a robust, background utility to automatically detect and click the "Skip Ad" button on YouTube using the Selenium web automation framework. It's designed to run continuously in the background, minimizing interruptions during your viewing experience.
- Continuous Ad Detection: Runs a non-blocking loop to constantly look for the skip button element.
- Multi-Strategy Skipping: Utilizes a list of known and likely locators (Class Name, CSS Selector) for maximum compatibility with various YouTube ad formats.
- Robust Click Logic: Implements JavaScript clicks as the primary method for reliable interaction, even when the element is visually obscured or prone to interception errors.
- Error Handling: Catches common Selenium exceptions (
TimeoutException,StaleElementReferenceException, etc.) to maintain stability. - Binary Auto-Detection: Automatically attempts to locate the Chromium/Chrome binary on common Linux/Unix paths.
- Graceful Shutdown: Cleans up the WebDriver instance upon a user-initiated
KeyboardInterrupt.
- Python: Version 3.6+
- Chromium/Chrome: The script requires a working installation of Google Chrome or Chromium. It attempts to find the binary at common locations (see
CHROMIUM_CANDIDATESin the script). - ChromeDriver: The corresponding ChromeDriver executable must be available in your system's PATH, or you must have the
selenium-managerdependency (which is bundled in recent Selenium versions) configured to handle it automatically.
Use pip to install the required library, Selenium:
pip install seleniumIf the script throws a RuntimeError regarding the binary not being found, you may need to update the CHROMIUM_CANDIDATES list in the script with the exact path to your browser executable.
Save the provided script as youtube_skipper.py and run it from your terminal:
python youtube_skipper.pyThe script will:
- Launch a maximized Chrome browser instance and navigate to
https://www.youtube.com. - Start the main loop, periodically checking for the "Skip Ad" button.
- Automatically skip ads as they appear, logging the action.
To stop the background process, return to the terminal window where the script is running and press:
The browser window will close automatically.
The following variables can be easily modified in the script:
| Variable | Description | Default Value |
|---|---|---|
CHROMIUM_CANDIDATES |
List of paths where the script looks for the Chrome/Chromium executable. | /usr/bin/chromium-browser, /usr/bin/chromium, /usr/bin/google-chrome |
WAIT_TIMEOUT |
The maximum time (in seconds) to wait for the ad skip button to appear. | 2 |
options.add_argument("--headless=new") |
Currently commented out. Uncomment this line to run the browser in the background without a visible window. | N/A (Commented) |
The script utilizes Python's logging module. All actions, including the initial YouTube opening, successful skips, and user interruptions, are logged to the console with timestamps for easy monitoring.
Example Log Output:
10:30:05 INFO: Opened YouTube
10:30:15 INFO: Ad skipped via By.CLASS_NAME: ytp-ad-skip-button-modern
10:32:01 INFO: Ad skipped after interception
10:35:45 INFO: Stopped by user