Skip to content

Instantly share code, notes, and snippets.

@ahmgeek
Forked from dannguyen/tweet-selenium.py
Last active March 21, 2016 03:01
Show Gist options
  • Save ahmgeek/f4872433d374f00ef721 to your computer and use it in GitHub Desktop.
Save ahmgeek/f4872433d374f00ef721 to your computer and use it in GitHub Desktop.
how to send a tweet through Firefox using Ruby + Selenium
#!/usr/bin/env ruby
require 'selenium-webdriver'
require 'rmagick'
include Magick
browser = Selenium::WebDriver.for :firefox
browser.get 'https://twitter.com'
element = browser.find_element(id: 'signin-email').send_keys('ahmgeek')
password = File.open('./pass.txt', 'r').read.strip
browser.find_element(id: 'signin-password').send_keys(password)
browser.find_element(css: '.front-signin .submit').click
wait = Selenium::WebDriver::Wait.new(:timeout => 2)
wait.until { browser.find_element(:id => "tweet-box-home-timeline") }
browser.find_element(id: 'tweet-box-home-timeline').send_keys('whoa I just tweeted from Firefox, using Ruby and Selenium!!, thanks to @dancow. https://goo.gl/YzvZ6J')
browser.save_screenshot('./captured.png')
img = Image.read('./captured.png').first.crop!(0, 0, 0, 400, true).write('/tmp/cropped_img.png')
browser.find_element(css: 'input.file-input').send_keys('/tmp/cropped_img.png')
browser.find_element(css: '.TweetBoxToolbar-tweetButton.tweet-button > button').click
# No exceptions handling, a way to hate my self..
# Also shamelessly tweaked from @dancow.
# * MAD HUGS * #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment