11"""
22Fingerprint Spoofing Feature Examples
33
4- Demonstrates how to use pydoll's fingerprint spoofing features to prevent browser fingerprint tracking.
4+ Demonstrates how to use pydoll's fingerprint spoofing features to prevent browser
5+ fingerprint tracking.
56"""
67
78import asyncio
9+ import traceback
10+
811from pydoll .fingerprint import FingerprintConfig , FingerprintManager
912from pydoll .fingerprint .browser import Chrome , Edge
1013
1114
1215async def basic_example ():
1316 """Basic example: Enable fingerprint spoofing with one click"""
1417 print ("=== Basic Example: Enable Fingerprint Spoofing ===" )
15-
18+
1619 # Create Chrome browser with fingerprint spoofing enabled
1720 browser = Chrome (enable_fingerprint_spoofing = True )
18-
21+
1922 async with browser :
2023 # Start browser
2124 tab = await browser .start ()
22-
25+
2326 # Visit fingerprint detection website
2427 await tab .go_to ("https://fingerprintjs.github.io/fingerprintjs/" )
25-
28+
2629 # Wait for page to load
2730 await asyncio .sleep (5 )
28-
31+
2932 # Get fingerprint ID
3033 try :
3134 fingerprint_element = await tab .find_element ("css" , ".visitor-id" )
@@ -34,21 +37,21 @@ async def basic_example():
3437 print (f"Generated fingerprint ID: { fingerprint_id } " )
3538 except Exception as e :
3639 print (f"Failed to get fingerprint ID: { e } " )
37-
40+
3841 # Take screenshot to save result
3942 try :
4043 await tab .take_screenshot ("fingerprint_result.png" )
4144 print ("Screenshot saved as fingerprint_result.png" )
4245 except Exception as e :
4346 print (f"Screenshot failed: { e } " )
44-
47+
4548 await asyncio .sleep (3 )
4649
4750
4851async def custom_config_example ():
4952 """Advanced example: Custom fingerprint configuration"""
5053 print ("\n === Advanced Example: Custom Fingerprint Configuration ===" )
51-
54+
5255 # Create custom fingerprint configuration
5356 config = FingerprintConfig (
5457 # Select features to spoof
@@ -59,37 +62,37 @@ async def custom_config_example():
5962 spoof_canvas = True ,
6063 spoof_audio = True ,
6164 spoof_webrtc = True ,
62-
65+
6366 # Custom settings
6467 browser_type = "chrome" ,
6568 os_type = "windows" ,
6669 device_type = "desktop" ,
6770 custom_languages = ["zh-CN" , "zh" , "en-US" , "en" ],
6871 custom_screen_resolution = (2560 , 1440 ),
6972 )
70-
73+
7174 # Create browser instance
7275 browser = Chrome (
7376 enable_fingerprint_spoofing = True ,
7477 fingerprint_config = config
7578 )
76-
79+
7780 async with browser :
7881 tab = await browser .start ()
79-
82+
8083 # Visit browser information detection website
8184 await tab .go_to ("https://browserleaks.com/javascript" )
82-
85+
8386 await asyncio .sleep (5 )
84-
87+
8588 # Get and print some browser information
8689 try :
8790 user_agent = await tab .execute_script ("return navigator.userAgent" )
8891 print (f"User Agent: { user_agent } " )
89-
92+
9093 platform = await tab .execute_script ("return navigator.platform" )
9194 print (f"Platform: { platform } " )
92-
95+
9396 screen_info = await tab .execute_script ("""
9497 return {
9598 width: screen.width,
@@ -100,109 +103,110 @@ async def custom_config_example():
100103 print (f"Screen info: { screen_info } " )
101104 except Exception as e :
102105 print (f"Failed to get browser information: { e } " )
103-
106+
104107 await asyncio .sleep (3 )
105108
106109
107110async def persistent_fingerprint_example ():
108111 """Persistent fingerprint example: Save and reuse fingerprints"""
109112 print ("\n === Persistent Fingerprint Example ===" )
110-
113+
111114 # Create fingerprint manager
112115 fingerprint_manager = FingerprintManager ()
113-
116+
114117 # Configure persistent fingerprint
115118 config = FingerprintConfig (
116119 persist_fingerprint = True ,
117120 fingerprint_id = "my_persistent_fingerprint"
118121 )
119-
122+
120123 # First use: Generate and save fingerprint
121124 print ("First visit: Generate new fingerprint" )
122125 browser1 = Chrome (
123126 enable_fingerprint_spoofing = True ,
124127 fingerprint_config = config
125128 )
126-
129+
127130 async with browser1 :
128131 tab = await browser1 .start ()
129132 await tab .go_to ("https://amiunique.org/fingerprint" )
130133 await asyncio .sleep (5 )
131-
134+
132135 # Get current fingerprint
133136 current_fingerprint = fingerprint_manager .current_fingerprint
134137 if current_fingerprint :
135138 print (f"Current User Agent: { current_fingerprint .user_agent } " )
136139 print (f"Current platform: { current_fingerprint .platform } " )
137-
140+
138141 # Second use: Load saved fingerprint
139142 print ("\n Second visit: Use same fingerprint" )
140-
143+
141144 # Load previously saved fingerprint
142145 saved_fingerprint = fingerprint_manager .load_fingerprint ("my_persistent_fingerprint" )
143146 if saved_fingerprint :
144147 print (f"Loaded User Agent: { saved_fingerprint .user_agent } " )
145148 print (f"Loaded platform: { saved_fingerprint .platform } " )
146-
149+
147150 # List all saved fingerprints
148151 all_fingerprints = fingerprint_manager .list_fingerprints ()
149152 print (f"\n All saved fingerprints: { list (all_fingerprints .keys ())} " )
150153
151154
152155async def multiple_browsers_example ():
153- """Multiple browsers example: Run multiple browsers with different fingerprints simultaneously"""
156+ """Multiple browsers example: Run multiple browsers with different fingerprints
157+ simultaneously"""
154158 print ("\n === Multiple Browsers Example ===" )
155-
159+
156160 # Create two browsers with different fingerprints
157161 browser1 = Chrome (enable_fingerprint_spoofing = True )
158162 browser2 = Chrome (enable_fingerprint_spoofing = True )
159-
163+
160164 async with browser1 , browser2 :
161165 # Start both browsers
162166 tab1 = await browser1 .start ()
163167 tab2 = await browser2 .start ()
164-
168+
165169 # Both visit the same fingerprint detection website
166170 await tab1 .go_to ("https://fingerprintjs.github.io/fingerprintjs/" )
167171 await tab2 .go_to ("https://fingerprintjs.github.io/fingerprintjs/" )
168-
172+
169173 await asyncio .sleep (5 )
170-
174+
171175 # Get fingerprint IDs from both browsers
172176 try :
173177 fp_element1 = await tab1 .find_element ("css" , ".visitor-id" )
174178 fp_element2 = await tab2 .find_element ("css" , ".visitor-id" )
175-
179+
176180 if fp_element1 and fp_element2 :
177181 fp_id1 = await fp_element1 .text
178182 fp_id2 = await fp_element2 .text
179-
183+
180184 print (f"Browser 1 fingerprint ID: { fp_id1 } " )
181185 print (f"Browser 2 fingerprint ID: { fp_id2 } " )
182-
186+
183187 if fp_id1 != fp_id2 :
184188 print ("✓ Success: Two browsers generated different fingerprints!" )
185189 else :
186190 print ("✗ Warning: Both browsers have the same fingerprint" )
187191 except Exception as e :
188192 print (f"Failed to get fingerprint ID: { e } " )
189-
193+
190194 await asyncio .sleep (3 )
191195
192196
193197async def edge_browser_example ():
194198 """Edge browser example"""
195199 print ("\n === Edge Browser Example ===" )
196-
200+
197201 # Create Edge browser with fingerprint spoofing enabled
198202 browser = Edge (enable_fingerprint_spoofing = True )
199-
203+
200204 async with browser :
201205 tab = await browser .start ()
202-
206+
203207 await tab .go_to ("https://www.whatismybrowser.com/" )
204208 await asyncio .sleep (5 )
205-
209+
206210 # Check browser identification
207211 try :
208212 browser_info = await tab .execute_script ("""
@@ -212,11 +216,11 @@ async def edge_browser_example():
212216 vendor: navigator.vendor
213217 }
214218 """ )
215-
219+
216220 print (f"Edge browser info: { browser_info } " )
217221 except Exception as e :
218222 print (f"Failed to get browser information: { e } " )
219-
223+
220224 await asyncio .sleep (3 )
221225
222226
@@ -225,22 +229,21 @@ async def main():
225229 try :
226230 # Run basic example
227231 await basic_example ()
228-
232+
229233 # Run custom configuration example
230234 await custom_config_example ()
231-
235+
232236 # Run persistent fingerprint example
233237 await persistent_fingerprint_example ()
234-
238+
235239 # Run multiple browsers example
236240 await multiple_browsers_example ()
237-
241+
238242 # Run Edge browser example
239243 await edge_browser_example ()
240-
244+
241245 except Exception as e :
242246 print (f"Error running examples: { e } " )
243- import traceback
244247 traceback .print_exc ()
245248
246249
0 commit comments