Timed out while uploading files using Playwright on Instagram automation in Google Colab

I’m attempting to automate the process of uploading videos to Instagram with Playwright in Google Colab. However, I’m encountering an issue with the file input element timing out. The script goes to the Instagram homepage and successfully clicks on the create post button, yet it fails to locate the file upload input element.

import asyncio
from playwright.async_api import async_playwright
import os

user_session = "abc123456789:sample_session_token:22:BXyz30Kpw4Sstp7UOCExrlftqsKgEErLIHj969vTxB"
video_path = "videos/test_video.mp4"
caption_text = "🚀 Automated upload test using Playwright #programming #webautomation"
os.makedirs("recordings", exist_ok=True)

async def upload_to_instagram():
    async with async_playwright() as playwright:
        browser = await playwright.chromium.launch(headless=False)
        context = await browser.new_context(
            record_video_dir="recordings",
            storage_state={
                "cookies": [{
                    "name": "sessionid",
                    "value": user_session,
                    "domain": ".instagram.com",
                    "path": "/",
                    "httpOnly": True,
                    "secure": True
                }]
            }
        )
        page = await context.new_page()
        await page.goto("https://www.instagram.com/", timeout=60000)
        print("Successfully loaded the page")
        
        # Locate and click create post button
        await page.wait_for_selector('[aria-label="New post"]', timeout=60000)
        await page.click('[aria-label="New post"]')
        print("Clicked the Create button")
        
        # Attempt to select post option
        try:
            await page.click('text=Post')
            print("Post option clicked")
        except:
            print("Post option not visible")
        
        # Failing point - file upload
        try:
            file_input = await page.wait_for_selector('input[type="file"]', timeout=60000)
            await file_input.set_input_files(video_path)
            print("Video file uploaded correctly")
        except Exception as e:
            print("Error with file upload:", e)
            await page.screenshot(path="upload_error.png")
            await browser.close()
            return
        
        await page.click('text=Next')
        await page.wait_for_timeout(3000)
        
        try:
            await page.fill("textarea[aria-label='Write a caption…']", caption_text)
        except:
            print("Failed to input caption")
        
        await page.click('text=Next')
        await page.wait_for_timeout(3000)
        await page.click('text=Share')
        print("Upload completed successfully")
        
        await browser.close()

await upload_to_instagram()

The issue I keep facing is a timeout while searching for the file input field. Has anyone experienced this before? I’m also open to using other automation frameworks if Playwright doesn’t seem ideal for this task. Any suggestions would be appreciated!

Instagram constantly changes their DOM structure, which breaks automation scripts. The file input might be hidden or buried deeper than you think. Try page.locator('input[type="file"]').first instead of wait_for_selector - there’s probably multiple file inputs and you need the visible one. What worked for me was waiting for the upload modal to fully load by checking for “Select from computer” text before hunting for the file input. Instagram also sometimes needs user interaction before showing the file picker, so click the upload area div first. Colab file paths are weird too - use absolute paths like /content/videos/test_video.mp4 so it can actually find your file. If you’re still getting timeouts, bump up the selector timeout and add backup selectors since Instagram’s interface is different for different accounts and regions.

Try page.set_input_files() on a broader selector instead of waiting for that specific input element. Instagram’s upload flow is weird - sometimes the file input won’t show up until you interact with something first. I’ve had better luck clicking await page.locator('[role="button"]:has-text("computer")').click() then uploading the file right away without waiting. Also, that session token looks fake but double-check it’s valid - Instagram hammers automated accounts pretty hard.

Had the exact same timeout issue building my Instagram bot. Instagram loads the file input dynamically after you open the create dialog, and the DOM changes depending on what state the UI’s in. Here’s what fixed it for me: add a wait condition before looking for the file input. After clicking create, Instagram sometimes shows an intermediate screen or takes time to render the upload interface. I added await page.wait_for_load_state('networkidle') right after clicking the new post button. Also, use a more specific selector like input[accept*="image"],input[accept*="video"] instead of just input[type="file"]. Instagram has multiple file inputs for different things and you want the media upload one specifically. One more thing - double-check your video file actually exists at that path in Colab. I wasted hours debugging selectors when the real issue was a wrong file path in the Colab environment.