Opening multiple tabs
New tabs vs new windows
Use
new_tab=True to open URLs in the same window, or new_window=True to create separate browser windows.Working with multiple tabs
Here’s a complete example that opens and manages multiple tabs:Iterating over tabs
The Browser object is iterable and provides multiple ways to access tabs:Parallel operations
Useasyncio.gather() to run operations on multiple tabs simultaneously:
1
Open multiple tabs
Create tabs using a loop or list comprehension:
2
Run operations in parallel
Use
asyncio.gather() for concurrent operations:3
Filter and organize tabs
Filter tabs by URL or other properties:
4
Clean up
Close tabs when done:
Window management
Control window size, position, and state:Advanced: Tiling windows
Automatic window tiling for better visualization:Real-world example: Coordinated scraping
Here’s a practical example that scrapes multiple pages in parallel:Tab activation and control
Key patterns
Keep reference to main tab
Safe tab closing
Parallel with error handling
Browser object as iterator
The Browser object provides several iteration patterns:Use
browser.tabs to get only page-type tabs, or iterate browser directly to include all target types (pages, iframes, workers, etc.).