Skip to content

Commit 8f02681

Browse files
authored
Merge pull request #37 from thalissonvs/doc-improvements
docs: improve documentation for clarity
2 parents 744bb6b + b7186a2 commit 8f02681

13 files changed

+1230
-115
lines changed

pydoll/browser/base.py

+82-9
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,36 @@ def __init__(
6060
self._pages = []
6161

6262
async def __aenter__(self):
63+
"""
64+
Async context manager entry point.
65+
66+
Returns:
67+
Browser: The browser instance.
68+
"""
6369
return self
6470

6571
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+
"""
6680
await self.stop()
6781
await self._connection_handler.close()
6882

6983
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+
"""
7193
binary_location = (
7294
self.options.binary_location or self._get_default_binary_location()
7395
)
@@ -108,6 +130,9 @@ async def get_page(self) -> Page:
108130
"""
109131
Retrieves a Page instance for an existing page in the browser.
110132
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.
111136
"""
112137
page_id = (
113138
await self.new_page() if not self._pages else self._pages.pop()
@@ -153,7 +178,9 @@ async def on(
153178
154179
Args:
155180
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.
157184
158185
Returns:
159186
int: The ID of the registered callback.
@@ -174,8 +201,12 @@ async def new_page(self, url: str = ''):
174201
"""
175202
Opens a new page in the browser.
176203
204+
Args:
205+
url (str): Optional initial URL to navigate to.
206+
Defaults to empty string.
207+
177208
Returns:
178-
Page: The new page instance.
209+
str: The ID of the new page.
179210
"""
180211
response = await self._execute_command(
181212
TargetCommands.create_target(url)
@@ -422,17 +453,40 @@ async def _continue_request_auth_required(
422453
await self.disable_fetch_events()
423454

424455
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+
"""
425465
pages = await self.get_targets()
426466
valid_page = await self._get_valid_page(pages)
427467
self._pages.append(valid_page)
428468

429469
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+
"""
431476
if not await self._is_browser_running():
432477
raise exceptions.BrowserNotRunning('Failed to start browser')
433478

434479
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+
"""
436490
if private_proxy:
437491
await self.enable_fetch_events(handle_auth_requests=True)
438492
await self.on(
@@ -452,17 +506,28 @@ async def _configure_proxy(self, private_proxy, proxy_credentials):
452506

453507
@staticmethod
454508
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+
"""
456518
return page.get('type') == 'page' and 'chrome://newtab/' in page.get(
457519
'url', ''
458520
)
459521

460522
async def _get_valid_page(self, pages) -> str:
461523
"""
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.
463528
464529
Returns:
465-
str: targetId da página existente ou nova
530+
str: The target ID of an existing or new page.
466531
"""
467532
valid_page = next(
468533
(page for page in pages if self._is_valid_page(page)), None
@@ -505,7 +570,15 @@ async def _execute_command(self, command: str):
505570
)
506571

507572
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+
"""
509582
temp_dir = self._temp_directory_manager.create_temp_dir()
510583
if '--user-data-dir' not in [
511584
arg.split('=')[0] for arg in self.options.arguments

pydoll/browser/chrome.py

+29
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,44 @@
77

88

99
class Chrome(Browser):
10+
"""
11+
A class that implements the Chrome browser functionality.
12+
13+
This class provides specific implementation for launching and
14+
controlling Google Chrome browsers.
15+
"""
16+
1017
def __init__(
1118
self,
1219
options: Optional[Options] = None,
1320
connection_port: Optional[int] = None,
1421
):
22+
"""
23+
Initializes the Chrome browser instance.
24+
25+
Args:
26+
options (Options | None): An instance of Options class to configure
27+
the browser. If None, default options will be used.
28+
connection_port (int): The port to connect to the browser.
29+
Defaults to 9222.
30+
"""
1531
super().__init__(options, connection_port)
1632

1733
@staticmethod
1834
def _get_default_binary_location():
35+
"""
36+
Gets the default location of the Chrome browser executable.
37+
38+
This method determines the default Chrome executable path based
39+
on the operating system.
40+
41+
Returns:
42+
str: The path to the Chrome browser executable.
43+
44+
Raises:
45+
ValueError: If the operating system is not supported or
46+
the browser executable is not found at the default location.
47+
"""
1948
os_name = platform.system()
2049
browser_paths = {
2150
'Windows':

0 commit comments

Comments
 (0)