2
import mechanize,cookielib
br=mechanize.Browser()
cookie_jar=cookielib.CookieJar()
br.set_cookiejar(cookie_jar)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
br.set_handle_referer(True)
br.set_handle_robots(False)
br.open("http://facebook.com/login.php")
br.select_form(nr=0)
br.form['email']=raw_input("Email please: ")
br.form['pass']=raw_input("Password please: ")
br.submit()
if "Logout" in br.response().read():
    print "Successfuly Logged in...May the Spam Process Begin ;) "
else:
    print "Something is wrong with logging in.Sorry :( "
link_to_spam=raw_input("Enter the messages link for the one you want to spam\n\
Example: https://www.facebook.com/messages/someone: ")
br.open(link_to_spam)
for f in br.forms():
    print f

So this code isn't showing the messages form in facebook.And don't comment on the purpose of the code.I'm only coding this for fun and won't use it actually except on my friends who accept that but even so I won't use it much. Does anyone have any idea about why it isn't showing?

Hasan Saad
  • 279
  • 4
  • 11

2 Answers2

7

I'm not sure if they use a form for the messages, it's likely javascript. Either way, this can be done with Selenium.

First, install Selenium: https://pypi.python.org/pypi/selenium

Then download the chrome driver from here: https://code.google.com/p/chromedriver/downloads/list

Put the binary in the same folder as the python script you're writing. (Or add it to the path or whatever, more information here: https://code.google.com/p/selenium/wiki/ChromeDriver)

Afterwards, the following code should work:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
email = raw_input("Email: ")
password = raw_input("Password: ")
person =raw_input("Enter the messages link for the one you want to spam\n\
Example: https://www.facebook.com/messages/someone: ")
driver.get("http://www.facebook.com")
elem = driver.find_element_by_id("email")
elem.send_keys(email)
elem = driver.find_element_by_id("pass")
elem.send_keys(password)
elem.send_keys(Keys.RETURN)
driver.get("https://facebook.com/messages/"+person)
elem = driver.find_element_by_css_selector("div textarea.uiTextareaNoResize")
elem.send_keys("python test")
elem.send_keys(Keys.RETURN)

It will actually open chrome in a new window and type text into the browser.

Matthew Wesly
  • 1,238
  • 1
  • 13
  • 14
0

Almost everything works but not:

elem = driver.find_element_by_css_selector("div textarea.uiTextareaNoResize")
elem.send_keys("python test")
elem.send_keys(Keys.RETURN)

There should be some other code instead of:

elem = driver.find_element_by_css_selector("div textarea.uiTextareaNoResize")