ruby script that logs in to twitter and updates profile picture
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Please supply a http://watirwebdriver.com/ ruby script that logs in to twitter and uploads a new profile picture.

awarded to MSF via vamsiikrishna
Tags
ruby
watir

Crowdsource coding tasks.

2 Solutions


require 'watir-webdriver'

# update with your credentials
username = "username"
password = "pass"
photo = "/path/to/photo.jpeg"


b = Watir::Browser.new :chrome

b.goto 'twitter.com'

b.text_field(:id => 'signin-email').set username
b.text_field(:id => 'signin-password').set password
b.button(:class => 'submit btn primary-btn flex-table-btn js-submit').click

b.goto 'twitter.com/settings/profile'
sleep(5)
b.button(:id => 'profile_image_upload').click
b.file_field(:name, "media[]").set photo
l = b.link :text => 'Save'
l.exists?
l.click

was kinda stuck for few mins with the upload part, documentation ftw !


Intro

I am assuming you have Watir installed, but if you don't run this command in terminal to install it:

gem install watir-webdriver

You might get a permissions error when trying to install, and in that case, sudo the command first.

sudo gem install watir-webdriver

Settings

There are three settings:

  1. Username
  2. Password
  3. Path to Image

Code

#!/usr/bin/ruby
require 'rubygems'
require 'watir-webdriver'

# settings
username = "USERNAME"
password = "PASSWORD"
imagepath = "/Users/alex/image.png"

# create new instance
browser = Watir::Browser.new
browser.goto    "https://twitter.com/"

# login
browser.text_field(:id => "signin-email").set username
browser.text_field(:id => "signin-password").set password
browser.button(:class => "js-submit").click

# change profile settings
browser.goto    "https://twitter.com/settings/profile"
sleep(1)
browser.button(:id => "profile_image_upload").click
browser.file_field(:class => "file-input").send_keys(imagepath)
sleep(2)
browser.link(:text => "Save").click

Enjoy!

View Timeline