Can't Login on Twitch with Selenium

Question:

I have a problem to login on twitch with selenium.
After the bot has entered the credentials (I also tried to enter them manually) the error message appears:
"Something went wrong. Please try again." And it won’t let me in.

Any suggestions?

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

def start_twitch_viewer():

    PATH = r"./Local/twitch-stream-viewer/chromedriver"
    email = '[email protected]'
    usr = 'Username'
    pswd = 'Password'


    chrome_options = webdriver.ChromeOptions()

    try:

        driver = webdriver.Chrome(PATH, options=chrome_options)
        driver.get("https://www.twitch.tv/ChannelName")
        driver.header_overrides = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"}

    except:
        return
    
    time.sleep(10)
    driver.find_element(By.CSS_SELECTOR, "div[class='Layout-sc-nxg1ff-0 csWXEI']").click()
    time.sleep(5)
    username=driver.find_element(By.CSS_SELECTOR, "input[id='login-username']")
    password=driver.find_element(By.CSS_SELECTOR, "input[id='password-input']")
    username.clear()
    password.clear()
    username.send_keys(usr)
    password.send_keys(pswd)
    time.sleep(5)
    driver.find_element(By.CSS_SELECTOR, "div[class='Layout-sc-nxg1ff-0 OZCSg']").click()
    

    time.sleep(1000)




if __name__ == "__main__":
    start_twitch_viewer()

EDIT:
This is the file based on the suggestion given by @Lenta.


    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("user-data-dir=/Users/usr/Library/Application Support/Google/Chrome")
    chrome_options.add_argument("profile-directory=Profile 3")
    chrome_options.add_experimental_option("detach", True)
    
    try:

        driver = webdriver.Chrome(executable_path=PATH, options=chrome_options)
        driver.set_window_position(0, 0)
        driver.set_window_size(1440, 900)
        driver.get("https://www.twitch.tv/user")
        
    except:
        return

Asked By: Piero

||

Answers:

I tried that and saw the Something went wrong. notification displayed.
I’m quite sure that site is blocking automated tools like Selenium to prevent boots there.
Try using undetected Selenium.

Answered By: Prophet

I believe certain Twitch users have been having issues logging in these past few days so it may be related. I had the "Something went wrong" error on Brave when trying to login but it worked on Edge. There’s this article reporting the issue from a couple of days ago:

https://piunikaweb.com/2022/09/30/twitch-users-unable-to-login-getting-something-went-wrong-error/

Answered By: RampantLion

To fix this issue "Something went wrong. Please try again." you can use your own chrome profile, but it’s better to create a new one to work with selenium, since the main profile will load all installed extensions that may not work with selenium.

How to create a new profile you can see here: How to open a Chrome Profile through Python

To add your profile use the following code:

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=O:\Users\Username\AppData\Local\Google\Chrome\User Data")
options.add_argument("profile-directory=profile_directory_name")
service = Service(executable_path='pathtoyourchromedriver.exe')
driver = webdriver.Chrome(service=service, options=options)

If you get the error "selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir" you need to close your main browser as it blocks the browser launching from selenium

Or you can use a profile from another browser such as firefox or edge but for this you need to install the necessary webdriver geckodriver or edgedriver.

To add your profile from firefox:

options = webdriver.FirefoxOptions()
options.add_argument("-profile")
options.add_argument("O:\Users\Username\AppData\Roaming\Mozilla\Firefox\Profiles\wfpwqtvd.default-release")
service = Service(executable_path='pathtoyourgeckodriver.exe')
driver = webdriver.Firefox(service=service, options=options)

To add your profile from edge:

options = webdriver.EdgeOptions()
options.add_argument("user-data-dir=O:\Users\Username\AppData\Local\Microsoft\Edge\User Data")
options.add_argument("profile-directory=profile_directory_name")
service = Service(executable_path='pathtoyourmsedgedriver.exe')
driver = webdriver.Edge(service=service, options=options)
Answered By: Brze

It looks like the www.twitch.tv website is using a Fastly security tool.

A quick DNS lookup shows that www.twitch.tv resolves to twitch.map.fastly.net.

They are most likely using the Signal Sciences product (https://www.signalsciences.com/about-us/news/fastly-to-acquire-signal-sciences/). I have encountered this on another website recently.

Typically rotating user agents and IP addresses (ideally using residential proxies) should do the trick. You want to load up the site with a "fresh" browser profile each time.

Answered By: Jeff Rainer