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?