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
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='path\to\your\chromedriver.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='path\to\your\geckodriver.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='path\to\your\msedgedriver.exe')
driver = webdriver.Edge(service=service, options=options)