Browser Automation
Cosine CLI can control a real Chrome browser instance using the Chrome DevTools Protocol (CDP). This enables web-based tasks like taking screenshots, navigating websites, filling forms, and extracting data from web pages.
What is CDP?
Section titled “What is CDP?”The Chrome DevTools Protocol (CDP) is a remote debugging protocol that allows external tools to control Chrome. Cosine CLI uses CDP to:
- Navigate websites - Load and interact with web pages
- Take screenshots - Capture full-page or element-specific screenshots
- Execute JavaScript - Run JS in the browser context
- Fill forms - Input data and interact with UI elements
- Extract data - Scrape information from web pages
Prerequisites
Section titled “Prerequisites”Before using browser automation, you need:
- Google Chrome installed on your system
- Chrome started with remote debugging enabled
Starting Chrome with Remote Debugging
Section titled “Starting Chrome with Remote Debugging”open -n -a "Google Chrome" --args --remote-debugging-port=9222 --user-data-dir=/tmp/cosine-chromeThis opens a new Chrome instance with:
--remote-debugging-port=9222- Enables CDP on port 9222--user-data-dir=/tmp/cosine-chrome- Isolated profile for Cosine
# For Google Chromegoogle-chrome --remote-debugging-port=9222 --user-data-dir=/tmp/cosine-chrome
# Or for Chromiumchromium --remote-debugging-port=9222 --user-data-dir=/tmp/cosine-chrome& "C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --user-data-dir=$env:TEMP\cosine-chromeConfiguring CDP in Cosine CLI
Section titled “Configuring CDP in Cosine CLI”Method 1: Config File (Recommended)
Section titled “Method 1: Config File (Recommended)”Add the CDP URL to your ~/.cosine.toml:
[browser]cdp_url = "http://localhost:9222"Now browser automation will be available in every session.
Method 2: Command-line Flag
Section titled “Method 2: Command-line Flag”Override the config per session:
cos start --cdp-url http://localhost:9222Or for one-shot tasks:
cos start --prompt "Go to example.com and take a screenshot" --cdp-url http://localhost:9222Using Browser Tools
Section titled “Using Browser Tools”Once configured, Cosine automatically detects the browser and exposes browser automation tools. The AI will suggest using the browser when relevant.
Example Tasks
Section titled “Example Tasks”Taking Screenshots:
You: Take a screenshot of my deployed app at https://myapp.comCosine: I'll navigate to your app and capture a screenshot.[Uses browser_navigate and browser_screenshot tools]Web Scraping:
You: Extract all the product names from https://shop.example.com/productsCosine: I'll visit the page and extract the product information for you.[Uses browser_navigate and browser_extract tools]Form Interaction:
You: Test the login form on https://myapp.com/loginCosine: I'll navigate to the login page and test the form.[Uses browser_navigate, browser_fill, browser_click tools]Browser Tools Reference
Section titled “Browser Tools Reference”When browser automation is enabled, Cosine has access to these tools:
| Tool | Description |
|---|---|
browser_navigate | Navigate to a URL |
browser_screenshot | Capture a screenshot |
browser_click | Click on an element |
browser_fill | Fill a form field |
browser_evaluate | Execute JavaScript |
browser_get_text | Extract text from the page |
browser_scroll | Scroll the page |
browser_wait | Wait for an element or timeout |
Security Considerations
Section titled “Security Considerations”User Approval
Section titled “User Approval”All browser actions require your approval by default (unless using --auto-accept). You’ll see:
- The URL being visited
- The action being performed
- Any data being sent
Isolated Profile
Section titled “Isolated Profile”Using a separate --user-data-dir is recommended because:
- Cosine’s browser sessions are isolated
- No access to your main Chrome cookies/logins
- Can be easily reset by deleting the temp directory
HTTPS and Security
Section titled “HTTPS and Security”- CDP communication is over HTTP (locally)
- Only use CDP on trusted networks
- The browser runs with your user permissions
Troubleshooting
Section titled “Troubleshooting”Browser Not Detected
Section titled “Browser Not Detected”If Cosine doesn’t detect the browser:
-
Verify Chrome is running with debugging:
Terminal window # Check if port 9222 is opencurl http://localhost:9222/json/version -
Verify the CDP URL is correct:
- Use
http://localhost:9222(notws://) - Don’t include
/jsonor other paths
- Use
-
Check for conflicting Chrome instances:
- Close all Chrome windows
- Start Chrome with the debugging flags shown above
Connection Refused
Section titled “Connection Refused”If you see “connection refused” errors:
- Make sure Chrome is actually running
- Verify the port matches (default is 9222)
- Check firewall settings
Page Load Failures
Section titled “Page Load Failures”If pages fail to load:
- Verify network connectivity
- Check if the URL is accessible in regular Chrome
- Look for SSL certificate issues
Advanced Configuration
Section titled “Advanced Configuration”Custom Chrome Binary
Section titled “Custom Chrome Binary”If Chrome is installed in a non-standard location:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 --user-data-dir=/tmp/cosine-chrome/usr/bin/google-chrome --remote-debugging-port=9222 --user-data-dir=/tmp/cosine-chromeMultiple Chrome Instances
Section titled “Multiple Chrome Instances”You can run multiple Chrome instances on different ports:
# First instance on port 9222chrome --remote-debugging-port=9222 --user-data-dir=/tmp/cosine-chrome-1
# Second instance on port 9223chrome --remote-debugging-port=9223 --user-data-dir=/tmp/cosine-chrome-2Then configure Cosine:
# Use first instancecos start --cdp-url http://localhost:9222
# Use second instancecos start --cdp-url http://localhost:9223Headless Mode
Section titled “Headless Mode”For servers or CI/CD environments, use headless mode:
chrome --headless --remote-debugging-port=9222 --user-data-dir=/tmp/cosine-chromeUsing Browser with MCP
Section titled “Using Browser with MCP”You can combine browser automation with MCP servers for powerful workflows:
{ "mcpServers": { "filesystem": { "transport": "stdio", "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/Users/pz/screenshots"] } }}Now you can:
You: Take a screenshot of the dashboard and save it to my screenshots folderCosine: I'll capture the screenshot and save it using the filesystem MCP server.Best Practices
Section titled “Best Practices”1. Start Chrome Before Cosine
Section titled “1. Start Chrome Before Cosine”Always start Chrome with debugging enabled before launching Cosine:
# Terminal 1: Start Chromeopen -n -a "Google Chrome" --args --remote-debugging-port=9222 --user-data-dir=/tmp/cosine-chrome
# Terminal 2: Start Cosinecos start2. Use Isolated Profiles
Section titled “2. Use Isolated Profiles”Keep Cosine’s browser sessions separate from your main browsing:
# Use a dedicated user data directory--user-data-dir=/tmp/cosine-chrome3. Close When Done
Section titled “3. Close When Done”Clean up the Chrome instance when finished:
# Find and kill the Chrome processpkill -f "remote-debugging-port=9222"
# Or remove the user data directoryrm -rf /tmp/cosine-chrome4. Monitor Resources
Section titled “4. Monitor Resources”Browser automation can be resource-intensive:
- Chrome uses significant RAM
- Close unused tabs
- Kill the Chrome process when done
Example Workflows
Section titled “Example Workflows”Visual Regression Testing
Section titled “Visual Regression Testing”You: Compare the current homepage design with the staging siteCosine: I'll take screenshots of both and compare them.Data Extraction
Section titled “Data Extraction”You: Extract pricing information from 5 competitor websitesCosine: I'll visit each site and extract their pricing tables.Form Testing
Section titled “Form Testing”You: Test the signup flow on our websiteCosine: I'll navigate through the signup process and report any issues.Next Steps
Section titled “Next Steps”- MCP Configuration - Add file system access for saving browser data
- Configuration - Learn about all config options
- CLI Overview - Return to CLI documentation index