class Browser: driver = None def __init__(self): capabilities = DesiredCapabilities.CHROME # capabilities["loggingPrefs"] = {"performance": "ALL"} # chromedriver < ~75 capabilities["goog:loggingPrefs"] = { "performance": "ALL" } # chromedriver 75+ self.driver = Chrome(CHROMEDRV_PATH, desired_capabilities=capabilities) def goto(self, url): self.driver.get(url) def set_input_value(self, name, value): self.driver.find_element_by_id(name).send_keys(value) def click_link(self, class_name): self.driver.find_element_by_class_name(class_name).click() def check_elem_exist(self, name): elem = None try: elem = self.driver.find_element_by_xpath(f"//*[text()='{name}']") finally: return elem def quit(self): self.driver.quit()
def delete_posts(user_email_address=None, user_password=None, user_profile_url=None): """ user_email_address: Your Email user_password: Your password user_profile_url: Your profile URL """ assert all((user_email_address, user_password, user_profile_url)), "Insufficient arguments provided" chrome_options = Options() prefs = {"profile.default_content_setting_values.notifications" : 2} chrome_options.add_experimental_option("prefs", prefs) chrome_options.add_argument("start-maximized") driver = Chrome(chrome_options=chrome_options) driver.implicitly_wait(10) driver.get("https://facebook.com") email = "email" password = "******" login="******" emailelement = driver.find_element_by_name(email) passwordelement = driver.find_element_by_name(password) emailelement.send_keys(user_email_address) passwordelement.send_keys(user_password) loginelement = driver.find_element_by_id(login) loginelement.click() driver.get(user_profile_url) 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\"]") 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") driver.execute_script("arguments[0].click();", confirmation_button) time.sleep(5) driver.refresh()
def delete_posts(user_email_address, user_password, user_profile_url, is_headless): """ user_email_address: str Your Email user_password: str Your password user_profile_url: str Your profile URL """ # The Chrome driver is required because Gecko was having issues chrome_options = Options() prefs = { "profile.default_content_setting_values.notifications": 2, 'disk-cache-size': 4096 } chrome_options.add_experimental_option("prefs", prefs) chrome_options.add_argument("start-maximized") if is_headless: chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('log-level=2') driver = Chrome(options=chrome_options) driver.implicitly_wait(10) driver.get("https://facebook.com") email = "email" password = "******" login = "******" emailelement = driver.find_element_by_name(email) passwordelement = driver.find_element_by_name(password) emailelement.send_keys(user_email_address) passwordelement.send_keys(user_password) loginelement = driver.find_element_by_id(login) loginelement.click() if "Two-factor authentication" in driver.page_source: # Allow time to enter 2FA code print("Pausing to enter 2FA code") time.sleep(20) print("Continuing execution") driver.get(user_profile_url) for _ in range(MAX_POSTS): post_button_sel = "_4xev" 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) driver.refresh()
minted_email = os.environ['minted_email'] except KeyError: minted_email = input("Enter your minted.com email address:") try: minted_password = os.environ['minted_password'] except KeyError: minted_password = input("Enter your minted.com password:") driver.get(url) # Selenium deals with lgin form email_elem = driver.find_element_by_name('email') email_elem.send_keys(minted_email) password_elem = driver.find_element_by_name('password') password_elem.send_keys(minted_password) login_submit = driver.find_element_by_class_name('loginButton') login_submit.click() sleep(5) # to load JS and be nice # Request address book contents as json response = driver.request('GET','https://addressbook.minted.com/api/contacts/contacts/?format=json') listings = response.json() # Create dataframe to hold addresses address_book = pd.DataFrame(listings) # Export to excel and csv address_book.to_excel('./data/minted-addresses-api.xlsx') address_book.to_csv('./data/minted-addresses-api.csv', index=False)
def delete_posts(user_email_address=None, user_password=None, user_profile_url=None): """ user_email_address: Your Email user_password: Your password user_profile_url: Your profile URL """ assert all((user_email_address, user_password, user_profile_url)), "Insufficient arguments provided" # The Chrome driver is required because Gecko was having issues chrome_options = Options() prefs = {"profile.default_content_setting_values.notifications": 2} chrome_options.add_experimental_option("prefs", prefs) chrome_options.add_argument("start-maximized") driver = Chrome(chrome_options=chrome_options) driver.implicitly_wait(10) driver.get("https://facebook.com") email = "email" password = "******" login = "******" emailelement = driver.find_element_by_name(email) passwordelement = driver.find_element_by_name(password) emailelement.send_keys(user_email_address) passwordelement.send_keys(user_password) loginelement = driver.find_element_by_id(login) loginelement.click() if "Two-factor authentication" in driver.page_source: # Allow time to enter 2FA code print("Pausing to enter 2FA code") time.sleep(20) print("Continuing execution") driver.get(user_profile_url) 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) # Required to sleep the thread for a bit after using JS to click this button time.sleep(5) driver.refresh()