Browse Source

Merge pull request #8 from mimischi/patch-1

Catch errors in all element selectors
pull/24/head
Wesley Kerfoot 6 years ago
committed by GitHub
parent
commit
8ec1b2e81f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      deletefb/deletefb.py

15
deletefb/deletefb.py

@ -6,9 +6,10 @@ import time
from seleniumrequests import Chrome from seleniumrequests import Chrome
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
MAX_POSTS = 5000 MAX_POSTS = 5000
SELENIUM_EXCEPTIONS = (NoSuchElementException, StaleElementReferenceException)
def run_delete(): def run_delete():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -104,6 +105,9 @@ def delete_posts(user_email_address,
for _ in range(MAX_POSTS): for _ in range(MAX_POSTS):
post_button_sel = "_4xev" post_button_sel = "_4xev"
while True:
try:
timeline_element = driver.find_element_by_class_name(post_button_sel) timeline_element = driver.find_element_by_class_name(post_button_sel)
actions = ActionChains(driver) actions = ActionChains(driver)
actions.move_to_element(timeline_element).click().perform() actions.move_to_element(timeline_element).click().perform()
@ -113,17 +117,18 @@ def delete_posts(user_email_address,
try: try:
delete_button = menu.find_element_by_xpath("//a[@data-feed-option-name=\"FeedDeleteOption\"]") delete_button = menu.find_element_by_xpath("//a[@data-feed-option-name=\"FeedDeleteOption\"]")
except SELENIUM_EXCEPTIONS:
# 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\"]") delete_button = menu.find_element_by_xpath("//a[@data-feed-option-name=\"HIDE_FROM_TIMELINE\"]")
actions.move_to_element(delete_button).click().perform() actions.move_to_element(delete_button).click().perform()
confirmation_button = driver.find_element_by_class_name("layerConfirm") confirmation_button = driver.find_element_by_class_name("layerConfirm")
# Facebook would not let me get focus on this button without some custom JS # Facebook would not let me get focus on this button without some custom JS
driver.execute_script("arguments[0].click();", confirmation_button) 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 # Required to sleep the thread for a bit after using JS to click this button
time.sleep(5) time.sleep(5)

Loading…
Cancel
Save