Skip to content

Instantly share code, notes, and snippets.

@vadviktor
Created January 24, 2021 09:21
Show Gist options
  • Save vadviktor/2754574b1c7f9d6f9b6de66bfd1eb70f to your computer and use it in GitHub Desktop.
Save vadviktor/2754574b1c7f9d6f9b6de66bfd1eb70f to your computer and use it in GitHub Desktop.
Delete all Google Photos with Selenium, Firefox, Python
"""
1. create a separate Firefox profile
2. start that profile and log into the Google Account of choice
3. get the profile's path from `about:profiles`
4. set `profile_url` to the profile's path
5. run script
"""
from time import sleep
from selenium import webdriver
from selenium.common.exceptions import TimeoutException, WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
profile_url = "/home/user/.mozilla/firefox/vhdacxyj.scraper"
photos_url = "https://photos.google.com"
wait_timeout = 10
checkbox_selector = "div.ckGgle"
delete_button_selector = 'button[title="Delete"]'
confirmation_button_selector = ".XfpsVe > button:nth-child(2)"
options = webdriver.FirefoxOptions()
options.headless = False
fp = webdriver.FirefoxProfile(profile_url)
wd = webdriver.Firefox(firefox_profile=fp, options=options)
wd.maximize_window()
wd.get(photos_url)
def waitfor_and_find_elements(wd, selector, wait_timeout):
return WebDriverWait(wd, wait_timeout).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, selector))
)
def waitfor_and_find_element(wd, selector, wait_timeout):
WebDriverWait(wd, wait_timeout).until(
EC.presence_of_element_located((By.CSS_SELECTOR, selector))
)
def select_items():
while True:
try:
waitfor_and_find_elements(wd, checkbox_selector, wait_timeout)
except TimeoutException:
wd.get(photos_url)
sleep(5)
continue
break
checkboxes = wd.find_elements_by_css_selector(checkbox_selector)
for cb in checkboxes:
try:
cb.click()
except WebDriverException:
continue
def reload():
"""
need to reload because dom changes
sleep to let the browser do its things
"""
sleep(3)
wd.get(photos_url)
sleep(5)
try:
while True:
select_items()
waitfor_and_find_element(wd, delete_button_selector, wait_timeout)
delete_button = wd.find_element_by_css_selector(delete_button_selector)
delete_button.click()
waitfor_and_find_element(wd, confirmation_button_selector, wait_timeout)
confirmation_button = wd.find_element_by_css_selector(
confirmation_button_selector
)
confirmation_button.click()
reload()
finally:
wd.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment