pythonselenium-webdriverautomationselenium-chromedriver

How To Expand You Tube Description Box With "...more" button


I am trying to create a Selenium script for a project where I want to check the description box of Youtube channel but despite my efforts I am unable to click "...more" button Correctly and extract the timestamps form youtube description

This is my Code

import sys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import re
import time

# Set up Selenium
options = webdriver.ChromeOptions()
options.add_argument('--headless')  # Run in headless mode
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)

def extract_timestamps(url, expand_selector="tp-yt-paper-button#expand", timeout=15):
    try:
        driver.get(url)
        time.sleep(5)  # Allow time for dynamic content to load

        # Locate the "Show More" button
        show_more_button = WebDriverWait(driver, timeout).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, expand_selector))
        )

        # Check if the button is visible and enabled
        if not show_more_button.is_displayed() or not show_more_button.is_enabled():
            print("Button is not interactable. Trying alternate methods.")
            driver.execute_script("arguments[0].scrollIntoView();", show_more_button)
            time.sleep(1)  # Stabilize after scrolling

        # Perform click using JavaScript if standard click fails
        try:
            show_more_button.click()
        except Exception:
            print("Standard click failed. Using JavaScript click.")
            driver.execute_script("arguments[0].click();", show_more_button)

        print("Successfully clicked 'Show More' button.")

        # Locate the expanded description
        description_element = WebDriverWait(driver, timeout).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#description-inline-expander"))
        )
        description = description_element.text
        print("Description:")
        print(description)

        # Extract timestamps using regex
        timestamps = re.findall(r'\d{1,2}:\d{2}', description)
        return description, timestamps
    except TimeoutException:
        print("Timeout while waiting for an element.")
        return "", []
    except Exception as e:
        print(f"Error extracting timestamps: {e}")
        return "", []
    finally:
        driver.quit()

# YouTube URL
youtube_url = "https://youtu.be/iTmlw3vQPSs"
description, timestamp_list = extract_timestamps(youtube_url)

# Output timestamps
if timestamp_list:
    print("Extracted Timestamps:")
    for timestamp in timestamp_list:
        print(timestamp)
else:
    print("No timestamps found in the description.")

# Save URL and timestamps to a file
file_name = "youtube_data.txt"
with open(file_name, "w") as file:
    file.write(f"URL: {youtube_url}\n")
    file.write(f"Timestamps: {', '.join(timestamp_list)}\n")

print(f"\nThe data has also been saved to {file_name}")

This is the Output I am getting

Button is not interactable. Trying alternate methods.
Standard click failed. Using JavaScript click.
Successfully clicked 'Show More' button.
Description:
This is description text having some timestamps.

 …
...more
No timestamps found in the description.

The data has also been saved to youtube_data.txt

I want to click this button below and extract the data

enter image description here


Solution

  • First of all, there are two buttons that match the css selector tp-yt-paper-button#expand.

    Secondly, sometimes Selenium doesn't really click for some reason. Using javascript will be better. Use this instead:

    driver.execute_script("document.querySelectorAll('tp-yt-paper-button#expand')[1].click();")