Skip to content

Instantly share code, notes, and snippets.

@momota10
Created July 2, 2017 08:34
Show Gist options
  • Save momota10/969d904b4cad239da2a5c00df1ad87e7 to your computer and use it in GitHub Desktop.
Save momota10/969d904b4cad239da2a5c00df1ad87e7 to your computer and use it in GitHub Desktop.
from selenium import webdriver
from getpass import getpass
def login_twitter(username, password):
driver = webdriver.Firefox()
driver.get("https://twitter.com/login")
username_field = driver.find_element_by_class_name("js-username-field")
password_field = driver.find_element_by_class_name("js-password-field")
username_field.send_keys(username)
driver.implicitly_wait(1)
password_field.send_keys(password)
driver.implicitly_wait(1)
driver.find_element_by_class_name("EdgeButtom--medium").click()
if __name__ == "__main__":
username = input("user name : ")
password = getpass("password : ")
login_twitter(username, password)
@jyto7034
Copy link

jyto7034 commented Oct 8, 2018

Thanks!

@itkhansunny
Copy link

Not working

@CP2020
Copy link

CP2020 commented Mar 8, 2020

not working

error:
NoSuchElementException: Message: Unable to locate element: .js-username-field

@EnhboldH
Copy link

not working

error:
NoSuchElementException: Message: Unable to locate element: .js-username-field

Because twitter is updated. You can use that code to select correct user input
driver.findElement(By.name("session[username_or_email]"))
driver.findElement(By.name("session[password]"))

@furkanuyar
Copy link

furkanuyar commented May 16, 2020

Actually in Python it should be

driver.find_element_by_name("session[username_or_email]")
driver.find_element_by_name("session[password]")

@Solin1998
Copy link

how to find login button?

Copy link

ghost commented Dec 31, 2020

You don't need the login button, instead you could submit after entering the password, like this

        ...
        password_field.send_keys(password)
        driver.implicitly_wait(1)
        password_field.submit()

@EnhboldH
Copy link

EnhboldH commented Apr 7, 2021

how to find login button?

Actually you can send newline character "\n" after your password.
for example:
username: "username"
password: "password\n"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment