Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
how to send a tweet through Firefox using Python + Selenium
Tweeting by controlling Firefox via Python + selenium
This script:
- Opens up Firefox
- Goes to
- Clicks the login button
- logs you in (assuming you have your twitter password in a file named `mypassword.txt`...)
- Fills out the Tweet box with some text
- Takes a screenshot of as it currently appears in your browser
- Attaches the screenshot to the Tweet
- Sends the tweet
Obviously you should be using tweepy + the Twitter API if you actually
want to programmatically work with Twitter data and be efficient about it
But it's fun watching Python control the browser interactively...
from selenium import webdriver
MY_SCREEN_NAME = 'yourtwitternamegoeshere'
# don't be an idiot by hardcoding your password in the actual script
MY_PASSWORD = open("mypassword.txt").read().strip()
driver = webdriver.Firefox()
## Login
el = driver.find_element_by_css_selector('button.Button.js-login')
# fill out the username
el = driver.find_element_by_css_selector("")
# "type" in your screen name
# fill out your password...again, another reason to use the API (with OAuth) and NOT
# your browser. At the very least, don't hardcode your password into the script
# "type" in your password
driver.find_element_by_css_selector(".LoginForm-password > input").send_keys(MY_PASSWORD)
# Submit the form
## Now write the tweet
# Select the tweet box
el = driver.find_element_by_id('tweet-box-home-timeline')
# might have to do this twice if it doesn't respond right away
el.send_keys("""whoa I just tweeted from Firefox, using Python and Selenium""")
# let's take a screenshot just to prove it
# crop and show only top 400 pixels because selenium will screenshot the ENTIRE page
from PIL import Image
img ="i-am-on-twitter.png")
# absolute filepath is needed
cropped_filename = "/tmp/cropped-i-am-on-twitter.png"
img.crop((0, 0, img.size[0], 400)).save(cropped_filename)
# Upload the image
# wait till image is uploaded until going forward
from import By
from import WebDriverWait
from import expected_conditions as EC
import sys
print("...uploading", cropped_filename)
WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'button.js-show-preview'))
print("Unexpected error:", sys.exc_info()[0])
# it was a successful upload...
# Now send the tweet by clicking the button
Copy link

dannguyen commented Mar 31, 2016

And here's what it looks like in action:


Copy link

crutchcorn commented Jul 1, 2016

Say, the attachment of a photo isn't working for me. Mind helping a stupid person out? xD

Copy link

crutchcorn commented Jul 1, 2016

More specifically Message: Element is not currently visible and so may not be interacted with. But I've tried sleep, and I've tried waiting for it... And nothing...

Copy link

divyakrithi commented Mar 15, 2019

Copy link

luizeduardoc commented Aug 2, 2019

It's not working for me, maybe twitter took out the 'tweet-box-home-timeline' id of the input box

Copy link

MashiroW commented Apr 17, 2020

That doesn t work anymore but thanks for sharing it

Copy link

ShiniM0d0ri commented May 4, 2021

use this element to upload image
tested in selenium 4.0.0.b3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment