-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitter Tweet Bot
66 lines (51 loc) · 2.2 KB
/
Twitter Tweet Bot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#################################### TWITTER TWEET BOT ####################################
#### REQUIREMENTS (version)
# Python (3)
# Selenium (3.13.0)
# Firefox (61.0.2)
from selenium import webdriver
from selenium.webdriver.common.by import By
import datetime, time, sys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
# tweet message field
tweet = str(input('Enter your tweet message: '))
# login credentials
email = input('Enter your Twitter login name: ')
password = input('Enter your Twitter password:')
# create a new firefoxProfile
profile = webdriver.FirefoxProfile()
# overides the FirefoxProfile configuration settings to prevent pop-up windows
profile.set_preference('dom.push.enabled',False)
profile.set_preference('dom.webnotifications.enabled',False)
print('Opening Twitter webpage...')
browser = webdriver.Firefox(executable_path='geckodriver',firefox_profile=profile)
browser.get('http://twitter.com')
wait = WebDriverWait(browser, 10)
print('Entering login credentials...')
browser.find_element(By.XPATH, '//input[@name="session[username_or_email]"]').send_keys(email)
browser.find_element(By.XPATH, '//input[@name="session[password]"]').send_keys(password)
# browser.find_element_by_xpath("//input[@name='session[username_or_email]']").send_keys(email)
# browser.find_element_by_xpath("//input[@name='session[password]']").send_keys(password)
print('Logging to Twitter...')
browser.find_element_by_xpath("//input[@type='submit']").click()
browser.implicitly_wait(10)
print('Inserting selecting quote to tweet field...')
tweetElem = browser.find_element_by_id('tweet-box-home-timeline').send_keys(tweet)
time.sleep(3)
# Tweets the quote
tweetButton = browser.find_element_by_css_selector('button.tweet-action')
#browser.implicitly_wait(5)
tweetButton.click()
# prints short tweet summary
print('-'*20)
print('Quote tweeted successfully!')
print('Tweet date:', datetime.datetime.now())
print('-'*20)
print('Your tweet:', tweet, '|','Length:',len(tweet))
print('-'*20)
# wait for 4 seconds and closes the automated browser session.
time.sleep(4)
print('Browser will close now...')
browser.quit()