Selenium WebDriver Authentication Issues with Twitch Platform

I’m having trouble with automated login using Selenium WebDriver on Twitch. When my script inputs the login details, I keep getting an error that says “Something went wrong. Please try again.” This happens even when I manually type the credentials myself.

Has anyone encountered this before? What could be causing this issue?

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

def twitch_login_bot():
    DRIVER_PATH = r"./chromedriver/chromedriver"
    user_email = '[email protected]'
    account_name = 'MyUsername'
    account_pass = 'MyPassword'
    
    browser_options = webdriver.ChromeOptions()
    
    try:
        browser = webdriver.Chrome(DRIVER_PATH, options=browser_options)
        browser.get("https://www.twitch.tv/SomeStreamer")
        browser.header_overrides = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"}
    except:
        return
    
    time.sleep(8)
    browser.find_element(By.CSS_SELECTOR, "div[class='Layout-sc-nxg1ff-0 csWXEI']").click()
    time.sleep(3)
    
    name_field = browser.find_element(By.CSS_SELECTOR, "input[id='login-username']")
    pass_field = browser.find_element(By.CSS_SELECTOR, "input[id='password-input']")
    
    name_field.clear()
    pass_field.clear()
    name_field.send_keys(account_name)
    pass_field.send_keys(account_pass)
    
    time.sleep(3)
    browser.find_element(By.CSS_SELECTOR, "div[class='Layout-sc-nxg1ff-0 OZCSg']").click()
    time.sleep(500)

if __name__ == "__main__":
    twitch_login_bot()

UPDATE: Here’s my modified version based on some suggestions I received:

browser_options = webdriver.ChromeOptions()
browser_options.add_argument("user-data-dir=/Users/myuser/Library/Application Support/Google/Chrome")
browser_options.add_argument("profile-directory=Profile 2")
browser_options.add_experimental_option("detach", True)

try:
    browser = webdriver.Chrome(executable_path=DRIVER_PATH, options=browser_options)
    browser.set_window_position(100, 100)
    browser.set_window_size(1200, 800)
    browser.get("https://www.twitch.tv/streamer")
except:
    return

I’ve hit this same authentication mess with Twitch before. Your issue is probably Twitch’s bot detection picking up on your automated patterns. You added the user agent header, but you’re doing it wrong - header_overrides doesn’t exist in Selenium WebDriver. Set the user agent through ChromeOptions before you initialize the driver.

Bigger problem: you’re using hardcoded delays and CSS selectors. Twitch changes their DOM structure all the time, so these selectors break constantly. Switch to better element identification and use WebDriverWait instead of sleep statements. Also, Twitch might be using canvas fingerprinting or behavioral analysis now, making automated logins way harder to pull off.

Twitch has multiple layers to catch automation, but I’ve gotten around this stuff before. Here’s what actually worked for me: Add --disable-web-security and --disable-features=VizDisplayCompositor to your Chrome options for stealth mode. Use ActionChains instead of send_keys - it mimics real typing with natural delays. Try undetected-chromedriver rather than regular ChromeDriver since it automatically patches most detection methods. Your profile loading idea is solid because it keeps cookies and session data, just make sure no other Chrome instance is using that same profile or you’ll get conflicts.

Twitch’s captcha kicks in when it spots Selenium. Add --disable-blink-features=AutomationControlled to your Chrome options and use execute_script to remove the webdriver property. Your login flow’s probably hitting rate limits too - throw in random delays between actions instead of using fixed sleep times.

Had this same issue about six months ago. Twitch ramped up their bot detection and your script’s triggering several red flags. You’re missing --disable-extensions and need to add --no-sandbox to your Chrome options. Those fixed sleep timers are a dead giveaway - switch to random intervals between 2-7 seconds. Also, setting headers after driver initialization won’t work. Move all header stuff into ChromeOptions before creating the webdriver. I’d suggest using requests-html for the auth flow instead of pure Selenium - leaves way fewer fingerprints.