|
11 | 11 | from pydoll.commands.storage import StorageCommands |
12 | 12 | from pydoll.connection import ConnectionHandler |
13 | 13 | from pydoll.element import WebElement |
14 | | -from pydoll.events.page import PageEvents |
15 | 14 | from pydoll.mixins.find_elements import FindElementsMixin |
16 | 15 | from pydoll.utils import decode_image_to_bytes |
17 | 16 |
|
@@ -134,12 +133,11 @@ async def go_to(self, url: str, timeout=300): |
134 | 133 | Args: |
135 | 134 | url (str): The URL to navigate to. |
136 | 135 | """ |
137 | | - await self._execute_command( |
138 | | - RuntimeCommands.evaluate_script( |
139 | | - f'window.location.assign("{url}");' |
140 | | - 'window.location.reload(true);' |
141 | | - ) |
142 | | - ) |
| 136 | + if await self._refresh_if_url_not_changed(url): |
| 137 | + return |
| 138 | + |
| 139 | + await self._execute_command(PageCommands.go_to(url)) |
| 140 | + |
143 | 141 | try: |
144 | 142 | await self._wait_page_load(timeout=timeout) |
145 | 143 | except asyncio.TimeoutError: |
@@ -379,35 +377,13 @@ async def _wait_page_load(self, timeout: int = 300): |
379 | 377 | """ |
380 | 378 | Waits for the page to finish loading. |
381 | 379 | """ |
382 | | - page_events_auto_enabled = False |
383 | | - |
384 | | - if not self._page_events_enabled: |
385 | | - page_events_auto_enabled = True |
386 | | - await self.enable_page_events() |
387 | | - |
388 | | - page_loaded = asyncio.Event() |
389 | | - |
390 | | - page_loaded_callback_id = await self.on( |
391 | | - PageEvents.PAGE_LOADED, |
392 | | - lambda _: page_loaded.set(), |
393 | | - temporary=True, |
394 | | - ) |
395 | | - |
396 | | - navigated_within_document_callback_id = await self.on( |
397 | | - PageEvents.NAVIGATED_WITHIN_DOCUMENT, |
398 | | - lambda _: page_loaded.set(), |
399 | | - temporary=True, |
400 | | - ) |
401 | | - |
402 | | - try: |
403 | | - await asyncio.wait_for(page_loaded.wait(), timeout=timeout) |
404 | | - except asyncio.TimeoutError: |
405 | | - pass |
406 | | - |
407 | | - await self._connection_handler.remove_callback(page_loaded_callback_id) |
408 | | - await self._connection_handler.remove_callback( |
409 | | - navigated_within_document_callback_id |
410 | | - ) |
411 | | - |
412 | | - if page_events_auto_enabled: |
413 | | - await self.disable_page_events() |
| 380 | + start_time = asyncio.get_event_loop().time() |
| 381 | + while True: |
| 382 | + response = await self._execute_command( |
| 383 | + RuntimeCommands.evaluate_script('document.readyState') |
| 384 | + ) |
| 385 | + if response['result']['result']['value'] == 'complete': |
| 386 | + break |
| 387 | + if asyncio.get_event_loop().time() - start_time > timeout: |
| 388 | + raise asyncio.TimeoutError('Page load timed out') |
| 389 | + await asyncio.sleep(0.5) |
0 commit comments