Solution Timeline

All versions (edits) of solutions to ruby script that logs in to twitter and updates profile picture appear below in the order they were created. Comments that appear under revisions were those created when that particular revision was current.

To see the revision history of a single solution (with diffs), click on the solution number (ie. "#1") in the upper right corner of a solution revision below.

← Bounty Expand all edits

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 'base64'
require 'open-uri'
require 'watir-webdriver'

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

# base64 encode image
image64 = open(imagepath) {|f| Base64.encode64(f.read) }
image64 = URI::encode(image64)
payload = "'image':'#{image64}'}"

# 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
sleep(2)
browser.goto    "javascript:(function(){$.post('https://api.twitter.com/1.1/account/update_profile_image.json', data: { #{payload} } );}());"

Enjoy!

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 'base64'
require 'open-uri'
require 'watir-webdriver'

# settings
username = "0x60"
password = "1651sierra"
imagepath = "/Users/alex/image.png"

# base64 encode image
image64 = open(imagepath) {|f| Base64.encode64(f.read) }
image64 = URI::encode(image64)
payload = "'image':'#{image64}'}"

# 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"
browser.button(:id => "profile_image_upload").click
browser.file_field(:class => "file-input").send_keys(imagepath)
browser.link(:id => "profile-image-save").click

Enjoy!

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
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 !

Winning solution

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 'base64'
require 'open-uri'
require 'watir-webdriver'

# settings
username = "0x60"
password = "XD"
imagepath = "/Users/alex/image.png"

# base64 encode image
image64 = open(imagepath) {|f| Base64.encode64(f.read) }
image64 = URI::encode(image64)
payload = "'image':'#{image64}'}"

# 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"
browser.button(:id => "profile_image_upload").click
browser.file_field(:class => "file-input").send_keys(imagepath)
browser.link(:id => "profile-image-save").click

Enjoy!

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 'base64'
require 'open-uri'
require 'watir-webdriver'

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

# base64 encode image
image64 = open(imagepath) {|f| Base64.encode64(f.read) }
image64 = URI::encode(image64)
payload = "'image':'#{image64}'}"

# 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
sleep(1)
browser.file_field(:class => "file-input").send_keys(imagepath)
sleep(2)
browser.link(:text => "Save").click

Enjoy!

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
sleep(1)
browser.file_field(:class => "file-input").send_keys(imagepath)
sleep(2)
browser.link(:text => "Save").click

Enjoy!

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!

Tipped