1

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('jok.moe@hotmail.com', '123456')
run.login()

Does anyone have any idea how to fix this ?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Joe Bek
  • 31
  • 1
  • 6

2 Answers2

2

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]"]') 
Jortega
  • 3,616
  • 1
  • 18
  • 21
2

Seems you were pretty close. To send a character sequence to the Email and Password fields, you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    class TwitterBot:
        def __init__(self,username,password):
            self.username = username
            self.password = password
            options = Options()
            options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
            self.bot = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
    
        def login(self):
            bot = self.bot
            bot.get('https://twitter.com/login')
            WebDriverWait(bot, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.js-username-field.email-input.js-initial-focus[name='session[username_or_email]']"))).send_keys(self.username)
            bot.find_element_by_css_selector("input.js-password-field[name='session[password]']").send_keys(self.password)
    
    run = TwitterBot('jok.moe@hotmail.com', '123456')
    run.login()
    
  • Using XPATH:

    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    class TwitterBot:
        def __init__(self,username,password):
            self.username = username
            self.password = password
            options = Options()
            options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
            self.bot = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
    
        def login(self):
            bot = self.bot
            bot.get('https://twitter.com/login')
            WebDriverWait(bot, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='js-username-field email-input js-initial-focus' and @name='session[username_or_email]']"))).send_keys(self.username)
            bot.find_element_by_xpath("//input[@class='js-password-field' and @name='session[password]']").send_keys(self.password)
    
    run = TwitterBot('jok.moe@hotmail.com', '123456')
    run.login()
    
  • Browser Snapshot:

twitter_login

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352