Is logging via selenium blocked by twitter?

51 Views Asked by At

Not able to login to twitter even with valid credentials using selenium. Elements accept the credentials, validate them but not redirecting to the home page after logging in.

Login details and main code:

email = 'twitter_email'
username = 'twitter_username'
password = 'twitter_password'
try:
    login_button = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'a[data-testid = loginButton]')))
    login_button.click()

    if driver.current_url == login_url:
        print('Login Page')
    else:
        print(f'Still at {base_url}')

    username_field = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'input[autocomplete = username]')))
    username_field.send_keys(username, Keys.RETURN)
    print('Username is valid')
    password_field = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'input[type = password]')))
    password_field.send_keys(password, Keys.RETURN)
    print('Password is valid')

    if wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'input[data-testid = ocfEnterTextTextInput]'))):
        email_field = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'input[data-testid = ocfEnterTextTextInput]')))
        email_field.send_keys(email, Keys.RETURN)
        print('Email verified')
    else:
        print('Email verification not required')

    # wait.until(lambda driver: driver.current_url != login_url)
    if driver.current_url == home_url:
        print('Logged in successfully!')
    else:
        print(f"Login failed! Current page: {driver.current_url}")

except TimeoutException:
    print('Login Failed due to timeout!')

Output:

Login Page
Username is valid
Password is valid
Email verified
Login failed! Current page: https://twitter.com/i/flow/login

Not able to reach home page even after successful login.

I have declared and concatenated my urls with base twitter url since the twitter url follows a pattern in creating urls based on the query by user.

base_url = 'https://twitter.com/'
login_element = 'i/flow/login'
home_element = 'home'
search_query = 'Random Query'
login_url = base_url + login_element
home_url = base_url + home_element
driver = gs.Chrome()
driver.get(base_url)
wait = WebDriverWait(driver, 30)

I was not able to find any elements available on home page of twitter, so checked for redirect by checking the current_url and login_url don't match with each other but it threw an error so commented it and used an if else block to check if driver is at the home page to my surprise it showed current page as login page even after logging in with correct credentials. I thought "current_url" shows the input provided to it and not the actual current page.

# wait.until(lambda driver: driver.current_url != login_url)
if driver.current_url == home_url:
    print('Logged in successfully!')
else:
    print(f"Login failed! Current page: {driver.current_url}")

So I tried starting from the base_url eventually reaching the login_url and well I concluded that current_url is the url the driver is currently at and not the input provided by me.

login_button = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'a[data-testid = loginButton]')))
login_button.click()

if driver.current_url == login_url:
    print('Login Page')
else:
    print('Still at base_url')

So the problem is only after logging by providing the credentials, since even after providing and entering the credentials and hitting return to login driver is still stuck at the login page. What am I doing wrong in the code by trying to login to twitter?

1

There are 1 best solutions below

3
Aariyan Patel On
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
 
username = 'username'
password = 'password'

 # Function to login to Twitter
    def login_to_twitter(username, password):
        driver = webdriver.Chrome()
        wait = WebDriverWait(driver, 10)
    
        try:
            # Load the login page
            driver.get("https://twitter.com/login")
    
            # Find the username input field and enter the username
            username_input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'input[name="text"]')))
            username_input.send_keys(username)
    
            login_button = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '[role=button].r-13qz1uu')))
            login_button.click()
    
            password_input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'input[name="password"]')))
            password_input.send_keys(password)
    
            # Find and click the login button
            login_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div[data-testid="LoginForm_Login_Button"]')))
            login_button.click()
    
            # Wait for the home page to load after successful login
            home_url = 'https://twitter.com/home'
            wait.until(EC.url_to_be(home_url))
            print('Logged in successfully!')
    
        except Exception as e:
            print('Login Failed:', e)
    
        finally:
            driver.quit()
    
    # Call the login function
    login_to_twitter(username, password)