| import asyncio |
| import os |
| from playwright.async_api import async_playwright |
| from typing import Optional |
|
|
| async def upload_file_to_chatgpt( |
| file_path: str, |
| headless: bool = False, |
| browser_path: Optional[str] = None |
| ) -> bool: |
| """ |
| Upload a file to ChatGPT using Playwright. |
| |
| Args: |
| file_path (str): Path to the file to upload |
| headless (bool): Whether to run browser in headless mode |
| browser_path (Optional[str]): Path to Chrome executable |
| |
| Returns: |
| bool: True if upload was successful, False otherwise |
| """ |
| if not os.path.exists(file_path): |
| raise FileNotFoundError(f"File not found: {file_path}") |
| |
| async with async_playwright() as p: |
| |
| browser = await p.chromium.launch( |
| headless=headless, |
| executable_path=browser_path |
| ) |
| |
| |
| context = await browser.new_context() |
| page = await context.new_page() |
| |
| try: |
| |
| await page.goto("https://chat.openai.com/") |
| |
| |
| upload_button = await page.wait_for_selector('input[type="file"]') |
| |
| |
| await upload_button.set_input_files(file_path) |
| |
| |
| await page.wait_for_timeout(2000) |
| |
| return True |
| |
| except Exception as e: |
| print(f"Error uploading file: {e}") |
| return False |
| |
| finally: |
| await browser.close() |
|
|
| |
| async def main(): |
| success = await upload_file_to_chatgpt("test.png") |
| print(f"Upload successful: {success}") |
|
|
| if __name__ == "__main__": |
| asyncio.run(main()) |