āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā š browser-use/code-agent/exporting-sessions ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
CodeAgent automatically saves all executed code and JavaScript blocks during your session. You can export your complete automation workflow in multiple formats for sharing, version control, or re-running later.
import asyncio
from browser_use import CodeAgent, ChatBrowserUse
from browser_use.code_use.notebook_export import export_to_ipynb, session_to_python_script
async def main():
agent = CodeAgent(
task="Extract product data from https://example.com",
llm=ChatBrowserUse(),
max_steps=10
)
# Run your automation
await agent.run()
# Export to Jupyter notebook
notebook_path = export_to_ipynb(agent, "product_scraping.ipynb")
# Export to Python script
python_script = session_to_python_script(agent)
with open("product_scraping.py", "w") as f:
f.write(python_script)
if __name__ == '__main__':
asyncio.run(main())
Contains:
Structure:
# Cell 1: Setup
import asyncio
import json
from browser_use import BrowserSession
from browser_use.code_use import create_namespace
browser = BrowserSession()
await browser.start()
namespace = create_namespace(browser)
globals().update(namespace)
# Cell 2: JavaScript variables
extract_products = """(function(){
return Array.from(document.querySelectorAll('.product')).map(product => ({
name: product.querySelector('.name')?.textContent,
price: product.querySelector('.price')?.textContent
}));
})()"""
# Remaining cells: Python execution
await navigate('https://example.com')
...
products = await evaluate(extract_products)
print(f"Found {len(products)} products")
Best for: Production deployment, version control, automation
Contains:
python script.pyStructure:
# Generated from browser-use code-use session
import asyncio
import json
from browser_use import BrowserSession
from browser_use.code_use import create_namespace
async def main():
# Initialize browser and namespace
browser = BrowserSession()
await browser.start()
# Create namespace with all browser control functions
namespace = create_namespace(browser)
# Extract functions from namespace for direct access
navigate = namespace["navigate"]
click = namespace["click"]
evaluate = namespace["evaluate"]
# ... other functions
# JavaScript Code Block: extract_products
extract_products = """(function(){
return Array.from(document.querySelectorAll('.product')).map(product => ({
name: product.querySelector('.name')?.textContent,
price: product.querySelector('.price')?.textContent
}));
})()"""
# Cell 1
await navigate('https://example.com')
# Cell 2
products = await evaluate(extract_products)
print(f"Found {len(products)} products")
await browser.stop()
if __name__ == '__main__':
asyncio.run(main())
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā