Interacting with inactive streamer profiles on Twitch using Selenium

I’m trying to navigate to the offline stream page of a Twitch user who isn’t live. When you visit an offline streamer’s page and click their avatar, it should redirect you to their offline stream instead of their main page. But I’m having trouble getting this to work with Selenium.

Here’s a sample of what I’ve tried so far:

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

browser = webdriver.Chrome()
browser.get('https://www.twitch.tv/someofflinestreamer')

try:
    avatar = WebDriverWait(browser, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, '.channel-avatar'))
    )
    avatar.click()
except:
    print('Failed to find or click the avatar')

browser.quit()

This code doesn’t work as expected. How can I modify it to successfully click on the avatar and navigate to the offline stream page? Any help would be appreciated!

hey jack, i’ve had issues with twitch too. their site is tricky! try using a different selector like ‘[data-a-target=“user-avatar”]’. also, add a longer wait time cuz twitch can be slow. if that doesn’t work, maybe try a different browser like firefox. good luck!

I’ve faced similar challenges when working with Selenium to interact with Twitch profiles. One issue you might be encountering is that Twitch’s UI can be quite dynamic, especially for offline streamers. Here’s what worked for me:

Instead of using the CSS selector ‘.channel-avatar’, try targeting the element more specifically. I found success using an XPath selector that looks for the avatar image within the channel header:

avatar = WebDriverWait(browser, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//div[contains(@class, "channel-header")]//img[@alt="Avatar"]'))
)

Also, after clicking the avatar, you might need to add a short wait to ensure the page has time to load. Something like:

WebDriverWait(browser, 10).until(
    EC.url_contains('/videos')
)

This waits for the URL to contain ‘/videos’, which is typically part of the offline stream page URL.

Remember to handle potential StaleElementReferenceExceptions, as Twitch’s dynamic content can sometimes cause elements to become stale. Wrapping your interactions in a retry loop can help with this.

Hope this helps you get your script working!