Created
June 5, 2020 22:35
-
-
Save pranshuj73/87fad1486655faf63ea6acf096fed9e8 to your computer and use it in GitHub Desktop.
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
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
import time | |
class Tweeter: | |
def __init__(self, username, password): | |
self.username = username | |
self.password = password | |
self.driver = webdriver.Chrome() # opens up the browser window | |
def login(self): | |
driver = self.driver | |
driver.get("https://twitter.com/login") # opens twitter login page | |
time.sleep(3) #sleeps for a while to let page load | |
# Now we need to find the field where we want to type in the username and password | |
uname_field = driver.find_element_by_name("session[username_or_email]") | |
pass_field = driver.find_element_by_name("session[password]") | |
# typing the username and the password | |
uname_field.send_keys(self.username, Keys.TAB) | |
pass_field.send_keys(self.password, Keys.RETURN) | |
print("Logged in as user:", self.username) | |
def tweet(self, tweet): | |
driver = self.driver | |
time.sleep(3) | |
tweet_btn = driver.find_element_by_xpath("//a[@aria-label=\"Tweet\"]") # finds the compose tweet button | |
tweet_btn.click() # to click on the button | |
time.sleep(2) | |
tweet_field = driver.find_element_by_xpath("//div[@aria-label=\"Tweet text\"]") # text field | |
tweet_field .click() | |
time.sleep(1) | |
tweet_field.send_keys(tweet) # This will type text in the text field | |
tweetbtn = driver.find_element_by_xpath("//*[contains(text(),'Tweet')]")# sends the tweet | |
tweetbtn.click() | |
print("Your tweet-", tweet, " has been tweeted!") | |
time.sleep(3) | |
def logout(self): | |
driver = self.driver | |
driver.get("https://twitter.com/logout") | |
time.sleep(2) | |
logout_btn = driver.find_element_by_xpath("//*[@id='react-root']/div/div/div[1]/div[2]/div/div/div/div[2]/div[2]/div[3]/div[2]")# finds the logout button | |
logout_btn.click() | |
print("You have been successfully logged out!") | |
driver.quit() # closes the browser window | |
tweeter = Tweeter("email/username", "password") | |
tweeter.login() | |
tweeter.tweet("Type your tweet here...") | |
tweeter.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment