Skip to content

Commit 9d6c941

Browse files
committed
docs: adding browser prefereces documentation
1 parent bd3f92e commit 9d6c941

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,34 @@ from pydoll.browser.options import ChromiumOptions
316316

317317
options = ChromiumOptions()
318318
options.binary_location = '/path/to/your/chrome'
319+
options.browser_preferences = {
320+
'download': {
321+
'default_directory': '/tmp/downloads',
322+
'prompt_for_download': False
323+
},
324+
'intl': {
325+
'accept_languages': 'en-US,en,pt-BR'
326+
},
327+
'profile': {
328+
'default_content_setting_values': {
329+
'notifications': 2 # Block notifications
330+
}
331+
}
332+
}
333+
334+
options.set_default_download_directory('/tmp/downloads')
335+
options.set_accept_languages('en-US,en,pt-BR')
336+
options.prompt_for_download = False
337+
319338
browser = Chrome(options=options)
320339
```
321340

341+
**Custom Preferences**
342+
- Set download directory, language, notification blocking, PDF handling, and more
343+
- Merge multiple calls; only changed keys are updated
344+
- See [docs/features.md](docs/features.md#custom-browser-preferences) for more details
345+
346+
322347
**Browser starts after a FailedToStartBrowser error?**
323348
```python
324349
from pydoll.browser import Chrome
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Deep Dive: Custom Browser Preferences in Pydoll
2+
3+
## Overview
4+
The `browser_preferences` feature (PR #204) enables direct, fine-grained control over Chromium browser settings via the `ChromiumOptions` API. This is essential for advanced automation, testing, and scraping scenarios where default browser behavior must be customized.
5+
6+
## How It Works
7+
- `ChromiumOptions.browser_preferences` is a dictionary that maps directly to Chromium's internal preferences structure.
8+
- Preferences are merged: setting new keys updates only those keys, preserving others.
9+
- Helper methods (`set_default_download_directory`, `set_accept_languages`, etc.) are provided for common scenarios.
10+
- Preferences are applied before browser launch, ensuring all settings take effect from the start of the session.
11+
- Validation ensures only dictionaries are accepted; invalid structures raise clear errors.
12+
13+
## Example
14+
```python
15+
options = ChromiumOptions()
16+
options.browser_preferences = {
17+
'download': {'default_directory': '/tmp', 'prompt_for_download': False},
18+
'intl': {'accept_languages': 'en-US,en'},
19+
'profile': {'default_content_setting_values': {'notifications': 2}}
20+
}
21+
```
22+
23+
## Advanced Usage
24+
- **Merging:** Multiple assignments merge keys, so you can incrementally build your preferences.
25+
- **Validation:** If you pass a non-dict or use the reserved 'prefs' key, an error is raised.
26+
- **Internals:** Preferences are set via a recursive setter that creates nested dictionaries as needed.
27+
- **Integration:** Used by the browser process manager to initialize the user data directory with your custom settings.
28+
29+
## Best Practices
30+
- Use helper methods for common patterns; set `browser_preferences` directly for advanced needs.
31+
- Check Chromium documentation for available preferences: https://chromium.googlesource.com/chromium/src/+/4aaa9f29d8fe5eac55b8632fa8fcb05a68d9005b/chrome/common/pref_names.cc
32+
- Avoid setting experimental or undocumented preferences unless you know their effects.
33+
34+
## References
35+
- See `pydoll/browser/options.py` for implementation details.
36+
- See tests in `tests/test_browser/test_browser_chrome.py` for usage examples.

docs/features.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,55 @@ These network analysis capabilities make Pydoll ideal for:
662662
- **Debugging**: Identify failed requests and network issues
663663
- **Security Testing**: Analyze request/response patterns
664664

665+
## Custom Browser Preferences
666+
667+
Pydoll now supports advanced browser customization through the `ChromiumOptions.browser_preferences` property. This allows you to set any Chromium browser preference for your automation session.
668+
669+
### What You Can Customize
670+
- Download directory and prompt behavior
671+
- Accepted languages
672+
- Notification blocking
673+
- PDF handling
674+
- Any other Chromium-supported preference
675+
676+
### Example: Setting Preferences
677+
```python
678+
from pydoll.browser.chromium import Chrome
679+
from pydoll.browser.options import ChromiumOptions
680+
681+
options = ChromiumOptions()
682+
options.browser_preferences = {
683+
'download': {
684+
'default_directory': '/tmp/downloads',
685+
'prompt_for_download': False
686+
},
687+
'intl': {
688+
'accept_languages': 'en-US,en,pt-BR'
689+
},
690+
'profile': {
691+
'default_content_setting_values': {
692+
'notifications': 2 # Block notifications
693+
}
694+
}
695+
}
696+
697+
# Helper methods for common preferences
698+
options.set_default_download_directory('/tmp/downloads')
699+
options.set_accept_languages('en-US,en,pt-BR')
700+
options.prompt_for_download = False
701+
702+
browser = Chrome(options=options)
703+
```
704+
705+
You can call `browser_preferences` multiple times—new keys will be merged, not replaced.
706+
707+
### Why is this useful?
708+
- Fine-grained control for scraping, testing, or automation
709+
- Avoid popups or unwanted prompts
710+
- Match user locale or automate downloads
711+
712+
---
713+
665714
## File Upload Support
666715

667716
Seamlessly handle file uploads in your automation:

0 commit comments

Comments
 (0)