Prerequisites
Before you begin, ensure you have:- Python 3.10 or higher
- Chrome or Chromium browser installed on your system
- Basic understanding of Python async/await syntax
Zendriver will automatically detect your Chrome installation. If you have Chrome installed in a non-standard location, you can specify the path when starting the browser.
Your first script
Let’s create a simple script that visits a website and captures a screenshot.Step 1: Install Zendriver
If you haven’t already, install Zendriver:Step 2: Create a Python file
Create a new file calledexample.py:
Step 3: Run the script
browserscan.png in your current directory.
Congratulations! You’ve successfully automated a browser with Zendriver.
Understanding the code
Let’s break down what each part does:Start the browser
start() function launches Chrome with sensible defaults. It returns a Browser object that you’ll use to control the browser.Navigate to a page
get() method navigates to a URL and returns a Tab object representing the page.Interact with the page
Tab object to interact with the page—in this case, saving a screenshot.Common patterns
Finding and clicking elements
Zendriver makes it easy to find elements by selector or text:Typing into input fields
Send keyboard input to form fields:Working with multiple elements
Find all matching elements:Browser configuration
Customize browser behavior with configuration options:By default, Zendriver runs in non-headless mode (showing the browser window). This helps avoid detection by anti-bot systems.
Next steps
Now that you understand the basics, explore these topics:Browser configuration
Learn about all available configuration options
Working with elements
Master element selection, interaction, and manipulation
Cookie management
Save and restore cookies for persistent sessions
API reference
Explore the complete API documentation
Troubleshooting
Chrome not found
Chrome not found
If Zendriver can’t find your Chrome installation, specify the path explicitly:
Element not found
Element not found
If
find() times out, the element might not exist or might load slowly:- Verify the selector is correct
- Increase the timeout:
await page.find("selector", timeout=30) - Check if the element is inside an iframe
Browser doesn't close
Browser doesn't close
Always call
await browser.stop() to clean up properly. Consider using a try/finally block: