Skip to content

Facebook message send automation using Watir-Webdriver

May 30, 2013

In my previous post I showed how to login and share a post in Facebook. This post will demonstrate how to send a message to friends in Facebook following some very simple steps.

First, we need to list down who we want to send the message to. If it’s a single person then we can set his/her name in the program. But if it’s a general message that we want to send to all our friends then typing all their names is not an option. So the friend list needs to be retrieved first.

Below code gets the name of the  friends from ‘Friends’ tab of Facebook and save that into a simple text file-

fbFriends = File.new("fbFriendList.txt", "w")

divs = browser.divs(:class, "fsl")

divs.each do |div|
 link = div.link(:index, 0)
 friends= link.text
 fbFriends.puts friends
end
fbFriends.close

This will create a text file named ‘fbFriendList’ into the same path where we ran the program from. Opening this text file will show all the person names who are friend with us in Facebook.

Now that we have got our friend list we can send message to them. Following code reads the friends name from ‘fbFriendList.txt’, set them as the recipient and send a general message to them.


browser.button(:value,"New Message").click
browser.text_field(:class, "inputtext textInput").flash

fbFriends = File.open("sampleFriend.txt")

while !fbFriends.eof?
 friends=fbFriends.readline
 browser.text_field(:class,"inputtext textInput").send_keys(friends)
 browser.text_field(:class,"inputtext textInput").send_keys :enter
end
fbFriends.close

browser.textarea(:index, 0).set("Please visit http://www.wateraid.org/uk and donate. Your £2 a month could help stop children dying from drinking dirty water.")
browser.button(:value,"Send").click

In case of sending a private message to a particular person we need to type the name of that friend instead of reading from the text file where all friends names are saved.


browser.button(:value,"New Message").click
browser.text_field(:class, "inputtext textInput").flash

browser.text_field(:class,"inputtext textInput").set("")  # Name of the friend goes here

browser.textarea(:index, 0).set("Please visit http://www.wateraid.org/uk and donate. Your £2 a month could help stop children dying from drinking dirty water.")
browser.button(:value,"Send").click

So the whole program from login into Facebook to message send and logout looks like this:


#****Sending message to all friends in Facebook

require "watir-webdriver"

signin={
 email: "", #The email that is used to sign in to Facebook
 password: "" #The password that is used to sign in to facebook
 }
event_URL_Friends="" #'Friends' tab's URL goes here
event_URL_Message="https://www.facebook.com/messages/"

browser = Watir::Browser.new
browser.goto "https://www.facebook.com/"

browser.text_field(:id, "email").set signin[:email]
browser.text_field(:id, "pass").set signin[:password]
browser.button(:type,"submit").click
browser.goto event_URL_Friends

fbFriends = File.new("fbFriendList.txt", "w")

divs = browser.divs(:class, "fsl")

divs.each do |div|
 link = div.link(:index, 0)
 friends= link.text
 fbFriends.puts friends
end
fbFriends.close

browser.goto event_URL_Message

browser.button(:value,"New Message").click
browser.text_field(:class, "inputtext textInput").flash

fbFriends = File.open("sampleFriend.txt")

while !fbFriends.eof?
 friends=fbFriends.readline
 browser.text_field(:class,"inputtext textInput").send_keys(friends)
 browser.text_field(:class,"inputtext textInput").send_keys :enter
end
fbFriends.close

browser.textarea(:index, 0).set("Please visit http://www.wateraid.org/uk and donate. Your £2 a month could help stop children dying from drinking dirty water.")
browser.button(:value,"Send").click
browser.link(:id, "navAccountLink").click
browser.button(:value,"Log out").click
browser.close

Advertisements

From → Watir

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: