Last active
December 14, 2022 07:45
-
-
Save dannguyen/8a6fa49253c1d6a0eb92 to your computer and use it in GitHub Desktop.
how to send a tweet through Firefox using Python + Selenium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Tweeting by controlling Firefox via Python + selenium | |
http://selenium-python.readthedocs.org/ | |
This script: | |
- Opens up Firefox | |
- Goes to https://www.twitter.com | |
- 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 twitter.com 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() | |
driver.get("https://www.twitter.com") | |
## Login | |
el = driver.find_element_by_css_selector('button.Button.js-login') | |
el.click() | |
# fill out the username | |
el = driver.find_element_by_css_selector("input.email-input") | |
# "type" in your screen name | |
el.send_keys(MY_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 | |
el.submit() | |
## Now write the tweet | |
# Select the tweet box | |
el = driver.find_element_by_id('tweet-box-home-timeline') | |
# ...you 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 | |
https://gist.github.com/dannguyen/8a6fa49253c1d6a0eb92""") | |
# let's take a screenshot just to prove it | |
driver.save_screenshot("i-am-on-twitter.png") | |
# crop and show only top 400 pixels because selenium will screenshot the ENTIRE page | |
from PIL import Image | |
img = Image.open("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 | |
driver.find_element_by_css_selector('input.file-input').send_keys(cropped_filename) | |
# wait till image is uploaded until going forward | |
# http://selenium-python.readthedocs.org/waits.html | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
import sys | |
try: | |
print("...uploading", cropped_filename) | |
WebDriverWait(driver, 5).until( | |
EC.presence_of_element_located((By.CSS_SELECTOR, 'button.js-show-preview')) | |
) | |
except: | |
print("Unexpected error:", sys.exc_info()[0]) | |
else: | |
# it was a successful upload... | |
# Now send the tweet by clicking the button | |
driver.find_element_by_css_selector('button.tweet-action').click() |
Say, the attachment of a photo isn't working for me. Mind helping a stupid person out? xD
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...
Nice and very useful.
selenium with python online training
It's not working for me, maybe twitter took out the 'tweet-box-home-timeline' id of the input box
That doesn t work anymore but thanks for sharing it
use this element to upload image
driver.find_element(By.CSS_SELECTOR,"input[data-testid='fileInput']").send_keys(<file>)
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
And here's what it looks like in action: https://twitter.com/dancow/status/714505327081693185