Skip to content

Instantly share code, notes, and snippets.

@debdutgoswami
Last active April 4, 2020 09:30
Show Gist options
  • Save debdutgoswami/5bb8a1a35198e49f8ffac957b0b99a35 to your computer and use it in GitHub Desktop.
Save debdutgoswami/5bb8a1a35198e49f8ffac957b0b99a35 to your computer and use it in GitHub Desktop.
Python script for sending out messages to various users. Just create a txt file with a list of usernames and then simply run the script. Note that this does not use Twitter API.
# imports
from selenium.webdriver import Chrome
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
# TWITTER
URL = "https://twitter.com"
# message
MESSAGE = "Hi {}, This is an automated message sent to you from Debdut Goswami"
# credentials
EMAIL = 'enter your email'
PWD = 'enter your password'
# fetch usernames from file
ftr = open('username.txt', 'r')
links = ftr.readlines()
ftr.close()
# status
status = list()
'''
NOTE: It is necessary to specify the path of the driver for Chrome or Firefox. I case you have it
in your python base directory, then you don't need to specify.
'''
browser = Chrome('chromedriver.exe')
# login to twitter
browser.get(URL)
time.sleep(2)
login = browser.find_element_by_name("session[username_or_email]")
login.send_keys(EMAIL+Keys.TAB+PWD+Keys.RETURN)
time.sleep(5)
for link in links:
uname = link.strip()
browser.get(URL+uname)
time.sleep(3)
# finding the DM button
try:
# finds the message button
message = browser.find_element_by_css_selector('#react-root > div > div > div > main > div > div > div > div.css-1dbjc4n.r-yfoy6g.r-18bvks7.r-1ljd8xs.r-13l2t4g.r-1phboty.r-1jgb5lz.r-11wrixw.r-61z16t.r-1ye8kvj.r-13qz1uu.r-184en5c > div > div:nth-child(2) > div > div > div:nth-child(1) > div > div.css-1dbjc4n.r-obd0qt.r-18u37iz.r-1w6e6rj.r-1wtj0ep > div > div:nth-child(2)')
ActionChains(browser).move_to_element(message).click().perform()
time.sleep(3)
# finds the textbox for typing the message
textbox = browser.find_element_by_css_selector('#react-root > div > div > div > main > div > div > div > section:nth-child(2) > div.css-1dbjc4n.r-1pz39u2.r-13awgt0 > div > div > div > div > aside > div.css-1dbjc4n.r-1awozwy.r-18u37iz.r-1sp51qo.r-ou255f.r-13qz1uu > div.css-1dbjc4n.r-16y2uox.r-1wbh5a2 > div > div > div.css-901oao.r-jwli3a.r-6koalj.r-16y2uox.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-qvutc0 > div > div > div > div.DraftEditor-editorContainer > div')
ActionChains(browser).move_to_element(textbox).click().perform()
time.sleep(1)
'''
We again need to find text box using x-path because it after we click on the textbox, it's
parent div changes it's class name (for styling obviously), so if we were to send_keys() to the
previous element then, it would give us an exception of NoSuchElementException
'''
new = browser.find_element_by_css_selector('#react-root > div > div > div > main > div > div > div > section:nth-child(2) > div.css-1dbjc4n.r-1pz39u2.r-13awgt0 > div > div > div > div > aside > div.css-1dbjc4n.r-1awozwy.r-18u37iz.r-1sp51qo.r-ou255f.r-13qz1uu > div.css-1dbjc4n.r-16y2uox.r-1wbh5a2 > div > div > div.css-901oao.r-jwli3a.r-6koalj.r-16y2uox.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-qvutc0 > div > div > div > div.DraftEditor-editorContainer > div > div > div > div > span')
MESSAGE = MESSAGE.format(uname)
new.send_keys(MESSAGE+Keys.RETURN)
status.append('YES')
except NoSuchElementException:
status.append('NO')
continue
time.sleep(10)
browser.close()
print(status)
'''
NOTE: Adjust the sleep time according to your internet speed. Don't reduce it too much,
else, it will be detected as a Bot.
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment