I tried following this tutorial https://www.youtube.com/watch?v=7ovFudqFB0Q but I get an error that element could not be found. I suspect that tweets = bot.find_element_by_class_name("tweet") does not work anymore since twitter changed some namings in the code. Let me know what would be a good alternative?
I tried selecting elements using CSS selector.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
class TwitterBot:
def __init__ (self, username, password):
self.username = username
self.password = password
self.bot = webdriver.Firefox()
def login(self):
bot = self.bot
bot.get("https://twitter.com/login")
time.sleep(3)
email = bot.find_element_by_class_name("js-username-field")
password = bot.find_element_by_class_name("js-password-field")
email.clear()
password.clear()
email.send_keys(self.username)
password.send_keys(self.password)
password.send_keys(Keys.RETURN)
time.sleep(3)
def like_tweet(self, hashtag):
bot = self.bot
bot.get('https://twitter.com/search?q=' + hashtag +'&src=typd')
time.sleep(3)
for i in range(1,3):
bot.execute_script('window.scrollTo(0,document.body.scrollHeight)')
time.sleep(2)
tweets = bot.find_element_by_class_name("tweet")
#tweets = bot.find_element_by_xpath('[data-testid="tweet"]')
#print(tweets)
links = [elem.get_attribute('data-permalink-path')
for elem in tweets]
#print(links)
ed = TwitterBot('xxxxxxx','xxxxx')
ed.login()
ed.like_tweet('xxxxx')
\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium-3.141.0-py3.8.egg\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .tweet
The example should return the list of all tweets on the page.