Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Catch errors in all element selectors #8

Merged
merged 3 commits into from
May 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 25 additions & 20 deletions deletefb/deletefb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
from seleniumrequests import Chrome
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options

from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException

MAX_POSTS = 5000
SELENIUM_EXCEPTIONS = (NoSuchElementException, StaleElementReferenceException)

def run_delete():
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -104,26 +105,30 @@ def delete_posts(user_email_address,

for _ in range(MAX_POSTS):
post_button_sel = "_4xev"
timeline_element = driver.find_element_by_class_name(post_button_sel)
actions = ActionChains(driver)
actions.move_to_element(timeline_element).click().perform()

menu = driver.find_element_by_css_selector("#globalContainer > div.uiContextualLayerPositioner.uiLayer > div")
actions.move_to_element(menu).perform()

try:
delete_button = menu.find_element_by_xpath("//a[@data-feed-option-name=\"FeedDeleteOption\"]")

# FIXME Using a bare except here to avoid having to handle all possible exceptions
except:
delete_button = menu.find_element_by_xpath("//a[@data-feed-option-name=\"HIDE_FROM_TIMELINE\"]")

actions.move_to_element(delete_button).click().perform()

confirmation_button = driver.find_element_by_class_name("layerConfirm")

# Facebook would not let me get focus on this button without some custom JS
driver.execute_script("arguments[0].click();", confirmation_button)
while True:
try:
timeline_element = driver.find_element_by_class_name(post_button_sel)
actions = ActionChains(driver)
actions.move_to_element(timeline_element).click().perform()

menu = driver.find_element_by_css_selector("#globalContainer > div.uiContextualLayerPositioner.uiLayer > div")
actions.move_to_element(menu).perform()

try:
delete_button = menu.find_element_by_xpath("//a[@data-feed-option-name=\"FeedDeleteOption\"]")
except SELENIUM_EXCEPTIONS:
delete_button = menu.find_element_by_xpath("//a[@data-feed-option-name=\"HIDE_FROM_TIMELINE\"]")

actions.move_to_element(delete_button).click().perform()
confirmation_button = driver.find_element_by_class_name("layerConfirm")

# Facebook would not let me get focus on this button without some custom JS
driver.execute_script("arguments[0].click();", confirmation_button)
except SELENIUM_EXCEPTIONS:
continue
else:
break

# Required to sleep the thread for a bit after using JS to click this button
time.sleep(5)
Expand Down