Search code examples
seleniumxpathtwittercss-selectorswebdriverwait

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element error sending text to Email field in twitter with Selenium Python


I am getting this error when i am trying to enter username and password automatically on twitter website using Firefox browser:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .session[username_or_email] 

The set of code i wrote so far is as follows:

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)
        bot.maximize_window()
        bot.implicitly_wait(3)
        email = bot.find_element_by_class_name('session[username_or_email]') 
        password = bot.find_element_by_class_name('session[password]')
        email.clear()
        password.clear()
        email.send_keys(self.username)
        password.send_keys(self.password)

run = TwitterBot('[email protected]', '123456')
run.login()

Does anyone have any idea how to fix this ?


Solution

  • The element looks like:

    <input class="js-username-field email-input js-initial-focus" type="text" name="session[username_or_email]" autocomplete="on" value="" placeholder="Phone, email or username">
    

    So you could do something like:

    email = bot.find_element_by_xpath('//input[@name="session[username_or_email]"]')