0

As you can see below that am trying to build small APP that login Twitter but i keep getting an error

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.Chrome()

    def login(self):
        bot = self.bot
        bot.get("https://twitter.com/login/")
        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)
password.send_keys(keys.RETURN)

Output:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=79.0.3945.88)
3
  • did you try using wait conditions? selenium-python.readthedocs.io/waits.html Commented Dec 26, 2019 at 1:04
  • Hi Omar, welcome to Stack Overflow community, kindly please share the HTML source for the element, also please note that error is usually resolvable by using implicitlyWait Commented Dec 26, 2019 at 1:12
  • 1
    The problem is not a waiting issue. He is selecting the wrong element on the page. Commented Dec 26, 2019 at 1:18

1 Answer 1

3

First of all, your code does not generate the stated error given when copy and pasted. I had to correct lines 6 and 22 to get it to run properly. Please be careful when putting code on here as it will discourage answers.

Your problem is that the session[username_or_email] is not a unique element on the Twitter login page. There are actually 3 elements with that name. three elements with the target name

You need to select the one which is actually interactable, which is the 2nd session[username_or_email] on the page. Same thing with session[password] Your code selects the first element on the page with that name. You are seeking the 2nd element on the page with that name.

You must change

email = bot.find_element_by_name('session[username_or_email]')
password = bot.find_element_by_name('session[password]')

to

email = bot.find_elements_by_name('session[username_or_email]')[1]
password = bot.find_elements_by_name('session[password]')[1]

The fully working code with all modifications

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.Chrome()
    def login(self):
        bot = self.bot
        bot.get("https://twitter.com/login/")
        time.sleep(3)

        email = bot.find_elements_by_name('session[username_or_email]')[1]
        password = bot.find_elements_by_name('session[password]')[1]

        email.clear()
        password.clear()
        email.send_keys(self.username)
        password.send_keys(self.password)
        password.send_keys(Keys.RETURN)

Omar = TwitterBot('Omar Username', 'Omar password')
Omar.login()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.