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!