Last active
September 10, 2024 15:57
-
-
Save adityatelange/9ebe8d816710496091e1ff77f6b78414 to your computer and use it in GitHub Desktop.
Youtube shorts history delete via web app (python + firefox + selenium) & ofc chatgpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from selenium import webdriver | |
# from selenium.webdriver.firefox.service import Service | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.common.by import By | |
# Path to your existing Firefox profile (get path using about:profiles in Firefox) | |
profile_path = "/home/user/.mozilla/firefox/wjobkgkx.default-release-1234567890123/" | |
# Set up Firefox options | |
options = webdriver.FirefoxOptions() | |
# options.add_argument("--headless") | |
options.add_argument("-profile") | |
options.add_argument(profile_path) | |
# Initialize the WebDriver with the specified profile | |
# service = Service('/home/user/bin/geckodriver') # Update this path to your geckodriver | |
driver = webdriver.Firefox(options=options) | |
# Now you can use the driver to interact with Firefox | |
driver.get("https://www.youtube.com/feed/history") | |
driver.implicitly_wait(3) # Wait up to 3 seconds for elements to be found | |
# Scroll and find elements | |
while True: | |
# Find all parent elements | |
parent_elements = driver.find_elements(By.TAG_NAME, "ytd-reel-shelf-renderer") | |
if not parent_elements: | |
print("No more 'ytd-reel-shelf-renderer' elements found.") | |
break # Exit the loop if no parent elements are found | |
# Iterate through each parent element to find the Next button | |
for parent in parent_elements: | |
while True: | |
child_elements = parent.find_elements(By.TAG_NAME, "ytd-reel-item-renderer") | |
if not child_elements: | |
child_elements = parent.find_elements(By.TAG_NAME, "ytm-shorts-lockup-view-model") | |
for child in child_elements: | |
vtitle = child.text | |
if vtitle: | |
print(f"[-] {vtitle}") | |
# Find the button with aria-label="Action menu" within the current child element | |
action_menu_button = child.find_element( | |
By.XPATH, ".//button" | |
) | |
# print("Found 'Action menu' button in:", child) | |
# Click the Action menu button | |
action_menu_button.click() | |
# time.sleep(1) # Wait for the menu to open | |
# Now find the tp-yt-paper-item/yt-formatted-string with the text 'Remove from watch history' | |
remove_from_watch_history = driver.find_element( | |
By.XPATH, | |
".//span[text()='Remove from watch history']", | |
) | |
# print("Found 'Remove from watch history' item.") | |
# Click on the 'Remove from watch history' item | |
remove_from_watch_history.click() | |
# time.sleep(1) # Wait for the action to complete | |
try: | |
# Find the button with aria-label="Next" within the current parent element | |
next_button = parent.find_element( | |
By.XPATH, ".//button[@aria-label='Next']" | |
) | |
# print("Found 'Next' button in:", parent) | |
# Click the button | |
next_button.click() | |
# time.sleep(1) # Wait for a second after clicking to allow content to load | |
except Exception as e: | |
# print("Next button not found in this parent or it has disappeared:", e) | |
break # Exit the inner loop if the button is not found | |
# Scroll down to the bottom of the page | |
driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);") | |
# Wait for new content to load | |
time.sleep(2) # Adjust the sleep time as necessary | |
# Check for new 'ytd-reel-shelf-renderer' elements | |
new_parent_elements = driver.find_elements(By.TAG_NAME, "ytd-reel-shelf-renderer") | |
if len(new_parent_elements) == len(parent_elements): | |
print("No new 'ytd-reel-shelf-renderer' elements found after scrolling.") | |
break # Exit the loop if no new parent elements are loaded | |
# close the driver when done | |
driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment