-4

I am making a Twitter bot that can automatically login when I run the script. But whenever I run the script, I get this an error that I cannot find any solutions for. Does anyone have an idea of how to fix it?

I tried to change element to elements and send_keys to send_Keys but it won't work

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/')
        time.sleep(3)
        email = bot.find_elements_by_class_name('email-input')
        password = bot.find_elements_by_class_name('session[password]')
        email.clear()
        password.clear()
        email.send_keys(self.username)
        password.send_keys(self.password)
        password.send_keys(Keys.RETURN)

ed = TwitterBot('EMAIL HERE', 'PASSWORD HERE')
ed.login()

I hope to get it logging in so I can work further on my project.

6
  • 2
    Evidently email and/or password is a list. Did you check what the find method returns? You probably need to get the first element of the list. Commented Jul 12, 2019 at 12:02
  • it returned the class names Commented Jul 12, 2019 at 12:03
  • Think, if it returns a list, does email.send_keys(self.username) make sense? Commented Jul 12, 2019 at 12:07
  • this is what i got Traceback (most recent call last): File "TwitterBot.py", line 24, in <module> ed.login() File "TwitterBot.py", line 19, in login email.send_keys(self.username) AttributeError: 'list' object has no attribute 'send_keys' Commented Jul 12, 2019 at 12:09
  • The first thing you should always do is to google the error message. If you do that, most of the time you will find a solution. Commented Jul 12, 2019 at 14:44

2 Answers 2

3

find_elements_by_xxx will return list of elements and you can't perform the send_keys operation on the list. Instead you have to use find_element_by_xxx which will return a single element, then you can perform element based operations.

If you want to get the list of element and then perform the operation on any specific element then you can use below logic.

elements = driver.find_elements_by_xxx("locator")
# perform operation on the first matching element
elements[0].send_keys("value_goes_here")
# if you want to perform operation on the last matching element
element[-1].send_keys("value_goes_here")
Sign up to request clarification or add additional context in comments.

3 Comments

If i use element then i get back this error selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .session[username_or_email]
Check if your locator is correct and make sure you have presence of element using WebDriverWait and ExpectedConditions.
this syntax is wrong for referencing elements in a list. it should use brackets elements[0]..
0

I now know what I messed up:

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/')
        time.sleep(3)
        email = bot.find_element_by_name('session[username_or_email]')
        password = bot.find_element_by_name('session[password]')
        email.clear()
        password.clear()
        email.send_keys(self.username)
        password.send_keys(self.password)

ed = TwitterBot('EMAIL HERE', 'PASSWORD HERE')
ed.login()

On the line email = bot.find_element_by_name('session[username_or_email]') it was first bot.find_element_by_class_name('session[username_or_email]')

Feeling stupid. Thanks for the help guys!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.