Skip to content

Commit 8e40491

Browse files
authored
Merge pull request #24 from thalissonvs/feat/handle-dialog
Feat: Handle javascript dialogs
2 parents 203d960 + f98555a commit 8e40491

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

pydoll/browser/page.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,39 @@ async def delete_all_cookies(self):
126126
await self._execute_command(StorageCommands.clear_cookies())
127127
await self._execute_command(NetworkCommands.clear_browser_cookies())
128128

129+
async def has_dialog(self) -> bool:
130+
"""
131+
Checks if a dialog is present on the page.
132+
133+
Returns:
134+
bool: True if a dialog is present, False otherwise.
135+
"""
136+
if self._connection_handler.dialog:
137+
return True
138+
return False
139+
140+
async def get_dialog_message(self) -> str:
141+
"""
142+
Retrieves the message of the dialog on the page.
143+
144+
Returns:
145+
str: The message of the dialog.
146+
"""
147+
if not await self.has_dialog():
148+
raise LookupError('No dialog present on the page')
149+
return self._connection_handler.dialog['params']['message']
150+
151+
async def accept_dialog(self):
152+
"""
153+
Accepts the dialog on the page.
154+
155+
Raises:
156+
LookupError: If no dialog is present on the page.
157+
"""
158+
if not await self.has_dialog():
159+
raise LookupError('No dialog present on the page')
160+
await self._execute_command(PageCommands.handle_dialog(True))
161+
129162
async def go_to(self, url: str, timeout=300):
130163
"""
131164
Navigates to a URL in the page.

pydoll/commands/page.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ class PageCommands:
3030
'method': 'Page.setDownloadBehavior',
3131
'params': {},
3232
}
33+
HANDLE_DIALOG = {'method': 'Page.handleJavaScriptDialog', 'params': {}}
34+
35+
@classmethod
36+
def handle_dialog(cls, accept: bool = True) -> dict:
37+
"""
38+
Generates the command to handle a JavaScript dialog.
39+
40+
Args:
41+
accept (bool): Whether to accept the dialog.
42+
If True, the dialog will be accepted.
43+
If False, the dialog will be dismissed.
44+
45+
Returns:
46+
dict: The command to be sent to the browser,
47+
containing the method and parameters for handling the dialog.
48+
"""
49+
command = cls.HANDLE_DIALOG.copy()
50+
command['params']['accept'] = accept
51+
return command
3352

3453
@classmethod
3554
def set_download_path(cls, path: str) -> dict:

pydoll/connection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def __init__(self, connection_port: int, page_id: str = 'browser'):
3838
self._callback_id = 0
3939
self._pending_commands: dict[int, asyncio.Future] = {}
4040
self.network_logs = []
41+
self.dialog = {}
4142
logger.info('ConnectionHandler initialized.')
4243

4344
@property
@@ -228,6 +229,12 @@ async def _handle_event(self, event: dict):
228229
self.network_logs.append(event)
229230
self.network_logs = self.network_logs[-10000:]
230231

232+
if 'Page.javascriptDialogOpening' in event_name:
233+
self.dialog = event
234+
235+
if 'Page.javascriptDialogClosed' in event_name:
236+
self.dialog = {}
237+
231238
event_callbacks = self._event_callbacks.copy()
232239
for callback_id, callback_data in event_callbacks.items():
233240
if callback_data['event'] == event_name:

0 commit comments

Comments
 (0)