@@ -60,14 +60,36 @@ def __init__(
60
60
self ._pages = []
61
61
62
62
async def __aenter__ (self ):
63
+ """
64
+ Async context manager entry point.
65
+
66
+ Returns:
67
+ Browser: The browser instance.
68
+ """
63
69
return self
64
70
65
71
async def __aexit__ (self , exc_type , exc_val , exc_tb ):
72
+ """
73
+ Async context manager exit point.
74
+
75
+ Args:
76
+ exc_type: The exception type, if raised.
77
+ exc_val: The exception value, if raised.
78
+ exc_tb: The traceback, if an exception was raised.
79
+ """
66
80
await self .stop ()
67
81
await self ._connection_handler .close ()
68
82
69
83
async def start (self ) -> None :
70
- """Método principal para iniciar o navegador."""
84
+ """
85
+ Main method to start the browser.
86
+
87
+ This method initializes the browser process and configures
88
+ all necessary settings to create a working browser instance.
89
+
90
+ Returns:
91
+ None
92
+ """
71
93
binary_location = (
72
94
self .options .binary_location or self ._get_default_binary_location ()
73
95
)
@@ -108,6 +130,9 @@ async def get_page(self) -> Page:
108
130
"""
109
131
Retrieves a Page instance for an existing page in the browser.
110
132
If no pages are open, a new page will be created.
133
+
134
+ Returns:
135
+ Page: A Page instance connected to an existing or new browser page.
111
136
"""
112
137
page_id = (
113
138
await self .new_page () if not self ._pages else self ._pages .pop ()
@@ -153,7 +178,9 @@ async def on(
153
178
154
179
Args:
155
180
event_name (str): Name of the event to listen for.
156
- callback (Callable): function to be called when the event occurs.
181
+ callback (callable): Function to be called when the event occurs.
182
+ temporary (bool): If True, the callback will be removed after it's
183
+ triggered once. Defaults to False.
157
184
158
185
Returns:
159
186
int: The ID of the registered callback.
@@ -174,8 +201,12 @@ async def new_page(self, url: str = ''):
174
201
"""
175
202
Opens a new page in the browser.
176
203
204
+ Args:
205
+ url (str): Optional initial URL to navigate to.
206
+ Defaults to empty string.
207
+
177
208
Returns:
178
- Page : The new page instance .
209
+ str : The ID of the new page.
179
210
"""
180
211
response = await self ._execute_command (
181
212
TargetCommands .create_target (url )
@@ -422,17 +453,40 @@ async def _continue_request_auth_required(
422
453
await self .disable_fetch_events ()
423
454
424
455
async def _init_first_page (self ):
456
+ """
457
+ Initializes the first page in the browser.
458
+
459
+ This method obtains the first valid page from available targets
460
+ and stores its ID for later use.
461
+
462
+ Returns:
463
+ None
464
+ """
425
465
pages = await self .get_targets ()
426
466
valid_page = await self ._get_valid_page (pages )
427
467
self ._pages .append (valid_page )
428
468
429
469
async def _verify_browser_running (self ):
430
- """Verifica se o navegador está rodando."""
470
+ """
471
+ Verifies if the browser is running.
472
+
473
+ Raises:
474
+ BrowserNotRunning: If the browser failed to start.
475
+ """
431
476
if not await self ._is_browser_running ():
432
477
raise exceptions .BrowserNotRunning ('Failed to start browser' )
433
478
434
479
async def _configure_proxy (self , private_proxy , proxy_credentials ):
435
- """Configura o proxy, se necessário."""
480
+ """
481
+ Configures proxy settings if needed.
482
+
483
+ Args:
484
+ private_proxy: Boolean indicating if a private proxy is enabled.
485
+ proxy_credentials: Tuple containing proxy username and password.
486
+
487
+ Returns:
488
+ None
489
+ """
436
490
if private_proxy :
437
491
await self .enable_fetch_events (handle_auth_requests = True )
438
492
await self .on (
@@ -452,17 +506,28 @@ async def _configure_proxy(self, private_proxy, proxy_credentials):
452
506
453
507
@staticmethod
454
508
def _is_valid_page (page : dict ) -> bool :
455
- """Verifica se uma página é uma nova aba válida."""
509
+ """
510
+ Verifies if a page is a valid new tab.
511
+
512
+ Args:
513
+ page (dict): Dictionary containing page information.
514
+
515
+ Returns:
516
+ bool: True if the page is a valid new tab, False otherwise.
517
+ """
456
518
return page .get ('type' ) == 'page' and 'chrome://newtab/' in page .get (
457
519
'url' , ''
458
520
)
459
521
460
522
async def _get_valid_page (self , pages ) -> str :
461
523
"""
462
- Obtém o ID de uma página válida ou cria uma nova.
524
+ Gets the ID of a valid page or creates a new one.
525
+
526
+ Args:
527
+ pages (list): List of page dictionaries to check for validity.
463
528
464
529
Returns:
465
- str: targetId da página existente ou nova
530
+ str: The target ID of an existing or new page.
466
531
"""
467
532
valid_page = next (
468
533
(page for page in pages if self ._is_valid_page (page )), None
@@ -505,7 +570,15 @@ async def _execute_command(self, command: str):
505
570
)
506
571
507
572
def _setup_user_dir (self ):
508
- """Prepara o diretório de dados do usuário, se necessário."""
573
+ """
574
+ Prepares the user data directory if needed.
575
+
576
+ This method creates a temporary directory for browser data if
577
+ no user directory is specified in the browser options.
578
+
579
+ Returns:
580
+ None
581
+ """
509
582
temp_dir = self ._temp_directory_manager .create_temp_dir ()
510
583
if '--user-data-dir' not in [
511
584
arg .split ('=' )[0 ] for arg in self .options .arguments
0 commit comments