@@ -21,12 +21,6 @@ def __init__(self):
2121 self ._binary_location = ''
2222 self ._start_timeout = 10
2323 self ._browser_preferences = {}
24- self ._prompt_for_download = True
25- self ._block_popups = False
26- self ._password_manager_enabled = True
27- self ._block_notifications = False
28- self ._allow_automatic_downloads = False
29- self ._open_pdf_externally = False
3024
3125 @property
3226 def arguments (self ) -> list [str ]:
@@ -131,6 +125,25 @@ def _set_pref_path(self, path: list, value):
131125 d = d .setdefault (key , {})
132126 d [path [- 1 ]] = value
133127
128+ def _get_pref_path (self , path : list ):
129+ """
130+ Safely gets a nested value from self._browser_preferences.
131+
132+ Arguments:
133+ path -- List of keys representing the nested
134+ path (e.g., ['plugins', 'always_open_pdf_externally'])
135+
136+ Returns:
137+ The value at the given path, or None if path doesn't exist
138+ """
139+ d = self ._browser_preferences
140+ try :
141+ for key in path :
142+ d = d [key ]
143+ return d
144+ except (KeyError , TypeError ):
145+ return None
146+
134147 def set_default_download_directory (self , path : str ):
135148 """
136149 Set the default directory where downloaded files will be saved.
@@ -155,7 +168,7 @@ def set_accept_languages(self, languages: str):
155168
156169 @property
157170 def prompt_for_download (self ) -> bool :
158- return self ._prompt_for_download
171+ return self ._get_pref_path ([ 'download' , 'prompt_for_download' ])
159172
160173 @prompt_for_download .setter
161174 def prompt_for_download (self , enabled : bool ):
@@ -167,12 +180,11 @@ def prompt_for_download(self, enabled: bool):
167180 Arguments:
168181 enabled -- If True, Chrome will ask for confirmation before downloading.
169182 """
170- self ._prompt_for_download = enabled
171- self ._set_pref_path (['download' , 'prompt_for_download' ], self ._prompt_for_download )
183+ self ._set_pref_path (['download' , 'prompt_for_download' ], enabled )
172184
173185 @property
174186 def block_popups (self ) -> bool :
175- return self ._block_popups == 0
187+ return self ._get_pref_path ([ 'profile' , 'default_content_setting_values' , 'popups' ]) == 0
176188
177189 @block_popups .setter
178190 def block_popups (self , block : bool ):
@@ -184,14 +196,13 @@ def block_popups(self, block: bool):
184196 Arguments:
185197 block -- If True, pop-ups will be blocked (value = 0); otherwise allowed (value = 1).
186198 """
187- self ._block_popups = 0 if block else 1
188199 self ._set_pref_path (
189- ['profile' , 'default_content_setting_values' , 'popups' ], self . _block_popups
200+ ['profile' , 'default_content_setting_values' , 'popups' ], 0 if block else 1
190201 )
191202
192203 @property
193204 def password_manager_enabled (self ) -> bool :
194- return self ._password_manager_enabled
205+ return self ._get_pref_path ([ 'profile' , 'password_manager_enabled' ])
195206
196207 @password_manager_enabled .setter
197208 def password_manager_enabled (self , enabled : bool ):
@@ -203,16 +214,16 @@ def password_manager_enabled(self, enabled: bool):
203214 Arguments:
204215 enabled -- If True, the password manager is active.
205216 """
206- self ._password_manager_enabled = enabled
207- self ._set_pref_path (['profile' , 'password_manager_enabled' ], self ._password_manager_enabled )
208- self ._set_pref_path (
209- ['credentials_enable_service' ], self ._password_manager_enabled
210- ) # todo: colocar em outra propriedade
217+ self ._set_pref_path (['profile' , 'password_manager_enabled' ], enabled )
218+ self ._set_pref_path (['credentials_enable_service' ], enabled )
211219
212220 @property
213221 def block_notifications (self ) -> bool :
214222 block_notifications_true_value = 2
215- return self ._block_notifications == block_notifications_true_value
223+ return (
224+ self ._get_pref_path (['profile' , 'default_content_setting_values' , 'notifications' ])
225+ == block_notifications_true_value
226+ )
216227
217228 @block_notifications .setter
218229 def block_notifications (self , block : bool ):
@@ -225,15 +236,19 @@ def block_notifications(self, block: bool):
225236 block -- If True, notifications will be blocked (value = 2);
226237 otherwise allowed (value = 1).
227238 """
228- self ._block_notifications = 2 if block else 1
229239 self ._set_pref_path (
230240 ['profile' , 'default_content_setting_values' , 'notifications' ],
231- self . _block_notifications ,
241+ 2 if block else 1 ,
232242 )
233243
234244 @property
235245 def allow_automatic_downloads (self ) -> bool :
236- return self ._allow_automatic_downloads == 1
246+ return (
247+ self ._get_pref_path (
248+ ['profile' , 'default_content_setting_values' , 'automatic_downloads' ]
249+ )
250+ == 1
251+ )
237252
238253 @allow_automatic_downloads .setter
239254 def allow_automatic_downloads (self , allow : bool ):
@@ -246,15 +261,14 @@ def allow_automatic_downloads(self, allow: bool):
246261 allow -- If True, automatic downloads are allowed (value = 1);
247262 otherwise blocked (value = 2).
248263 """
249- self ._allow_automatic_downloads = 1 if allow else 2
250264 self ._set_pref_path (
251265 ['profile' , 'default_content_setting_values' , 'automatic_downloads' ],
252- self . _allow_automatic_downloads ,
266+ 1 if allow else 2 ,
253267 )
254268
255269 @property
256270 def open_pdf_externally (self ) -> bool :
257- return self ._open_pdf_externally
271+ return self ._get_pref_path ([ 'plugins' , 'always_open_pdf_externally' ])
258272
259273 @open_pdf_externally .setter
260274 def open_pdf_externally (self , enabled : bool ):
@@ -266,5 +280,4 @@ def open_pdf_externally(self, enabled: bool):
266280 Arguments:
267281 block -- If True, location access is blocked (value = 2); otherwise allowed (value = 1).
268282 """
269- self ._open_pdf_externally = enabled
270- self ._set_pref_path (['plugins' , 'always_open_pdf_externally' ], self ._open_pdf_externally )
283+ self ._set_pref_path (['plugins' , 'always_open_pdf_externally' ], enabled )
0 commit comments