Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions pydoll/browser/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,39 @@ async def delete_all_cookies(self):
await self._execute_command(StorageCommands.clear_cookies())
await self._execute_command(NetworkCommands.clear_browser_cookies())

async def has_dialog(self) -> bool:
"""
Checks if a dialog is present on the page.

Returns:
bool: True if a dialog is present, False otherwise.
"""
if self._connection_handler.dialog:
return True
return False

async def get_dialog_message(self) -> str:
"""
Retrieves the message of the dialog on the page.

Returns:
str: The message of the dialog.
"""
if not await self.has_dialog():
raise LookupError('No dialog present on the page')
return self._connection_handler.dialog['params']['message']

async def accept_dialog(self):
"""
Accepts the dialog on the page.

Raises:
LookupError: If no dialog is present on the page.
"""
if not await self.has_dialog():
raise LookupError('No dialog present on the page')
await self._execute_command(PageCommands.handle_dialog(True))

async def go_to(self, url: str, timeout=300):
"""
Navigates to a URL in the page.
Expand Down
19 changes: 19 additions & 0 deletions pydoll/commands/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ class PageCommands:
'method': 'Page.setDownloadBehavior',
'params': {},
}
HANDLE_DIALOG = {'method': 'Page.handleJavaScriptDialog', 'params': {}}

@classmethod
def handle_dialog(cls, accept: bool = True) -> dict:
"""
Generates the command to handle a JavaScript dialog.

Args:
accept (bool): Whether to accept the dialog.
If True, the dialog will be accepted.
If False, the dialog will be dismissed.

Returns:
dict: The command to be sent to the browser,
containing the method and parameters for handling the dialog.
"""
command = cls.HANDLE_DIALOG.copy()
command['params']['accept'] = accept
return command

@classmethod
def set_download_path(cls, path: str) -> dict:
Expand Down
7 changes: 7 additions & 0 deletions pydoll/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self, connection_port: int, page_id: str = 'browser'):
self._callback_id = 0
self._pending_commands: dict[int, asyncio.Future] = {}
self.network_logs = []
self.dialog = {}
logger.info('ConnectionHandler initialized.')

@property
Expand Down Expand Up @@ -228,6 +229,12 @@ async def _handle_event(self, event: dict):
self.network_logs.append(event)
self.network_logs = self.network_logs[-10000:]

if 'Page.javascriptDialogOpening' in event_name:
self.dialog = event

if 'Page.javascriptDialogClosed' in event_name:
self.dialog = {}

event_callbacks = self._event_callbacks.copy()
for callback_id, callback_data in event_callbacks.items():
if callback_data['event'] == event_name:
Expand Down
Loading