Skip to content

Instantly share code, notes, and snippets.

@gabriellima
Created October 28, 2011 18:20
Show Gist options
  • Save gabriellima/1322970 to your computer and use it in GitHub Desktop.
Save gabriellima/1322970 to your computer and use it in GitHub Desktop.
Remove all unwanted email from open mail (based on NeoMail)
from splinter.browser import Browser
from selenium.common.exceptions import StaleElementReferenceException
class SpamRemover(object):
"""This script will attempt to delete until no more mail from
given sender is found in view."""
def __init__(self, mail_url):
"""After browser is open, you need to login by yourself."""
self.b = Browser('chrome')
self.b.visit(mail_url)
def _delete_all(self, mails):
if mails:
for _input in mails:
_input.check()
self.b.find_by_css('input[name=allbox]').first.click
self.b.find_by_css('select[name=destination] option[value=DELETE]').first.check()
self.b.find_by_css('input[name=movebutton]').first.click()
self.b.get_alert().accept()
def _find_unwanted_from(self, senders):
delete_inputs = []
for sender in senders:
delete_inputs.extend(self.b.find_by_xpath("//tr[contains(@id, 'tr')][td//a[text()='%s']]//input[@name='message_ids']" % sender))
return delete_inputs
def delete_all_mail_from(self, sender, *args):
"""Specify senders names (exactly like the ones that appears in view)"""
senders = [sender,]
if args:
senders.extend(args)
try:
mails_to_delete = True
while mails_to_delete:
mails_to_delete = self._find_unwanted_from(senders)
if mails_to_delete: #ugly thing
self._delete_all(mails_to_delete)
except StaleElementReferenceException:
self.delete_all_mail_from(*senders) #recurse to bypass exception
print 'Finished!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment