Skip to content

Instantly share code, notes, and snippets.

@rash0
Created March 18, 2019 08:50
Show Gist options
  • Save rash0/50e7174cf1d6b737006f47805c930694 to your computer and use it in GitHub Desktop.
Save rash0/50e7174cf1d6b737006f47805c930694 to your computer and use it in GitHub Desktop.
Automat logging and tweeting to twitter using python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
MY_SCREEN_NAME = 'your email or username'
tweet = 'your tweet string'
# It is more secure to read your password from a file using `open("mypassword.txt").read().strip()` instead
MY_PASSWORD = 'your password'
# start a 'headless' firefox instance
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get("https://twitter.com/login")
# Filling the form inputs and logging in
driver.find_element_by_css_selector(".js-username-field.email-input.js-initial-focus").send_keys('MY_SCREEN_NAME')
driver.find_element_by_css_selector(".js-password-field").send_keys('MY_PASSWORD')
# log in button click
driver.execute_script('return document.documentElement.outerHTML')
d = driver.find_element_by_css_selector('.submit.EdgeButton.EdgeButton--primary.EdgeButtom--medium')
d.click()
# If the tweet is bigger than 280 characters, open a thread actions
if len(tweet) > 276:
# find the last space at characters count 270
space_location = tweet.find(' ', 270)
# first part of the tweet
first_part_tweet = tweet[:space_location] + ' ..'
# second part of the tweet
sec_part_tweet = tweet[space_location:]
# send tweet to textbox
driver.find_element_by_id('tweet-box-home-timeline').send_keys(first_part_tweet)
# wait for the rendering of thread button
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div/div[2]/div[2]/div/form/div[3]/div[2]/span/button")))
# select thread button
thread_btn = driver.find_element_by_css_selector('form.tweet-form:nth-child(2) > div:nth-child(3) > div:nth-child(2) > span:nth-child(1) > button:nth-child(1)')
thread_btn.click()
# input second part of the tweet
driver.find_element_by_xpath('/html/body/div[34]/div[2]/div[2]/div[2]/div[4]/div[2]/div[1]/div[2]/div[2]/div[2]/div[1]').send_keys(sec_part_tweet)
# wait for the render of tweet all button
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[34]/div[2]/div[2]/div[3]/div/button[2]")))
# tweet all button click
tweet_all_btn = driver.find_element_by_css_selector('div.buttons > button:nth-child(2)')
tweet_all_btn.click()
else:
driver.find_element_by_id('tweet-box-home-timeline').send_keys(tweet)
# wait until the button appear
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div/div[2]/div[2]/div/form/div[3]/div[2]/button")))
tweet_button = driver.find_element_by_css_selector('form.tweet-form:nth-child(2) > div:nth-child(3) > div:nth-child(2) > button:nth-child(2)')
# tweet
tweet_button.click()
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment