Skip to content

Commit 9cfa4cd

Browse files
committed
fix: refactor page navigation and loading logic for efficiency
1 parent 606ca32 commit 9cfa4cd

File tree

1 file changed

+15
-39
lines changed

1 file changed

+15
-39
lines changed

pydoll/browser/page.py

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pydoll.commands.storage import StorageCommands
1212
from pydoll.connection import ConnectionHandler
1313
from pydoll.element import WebElement
14-
from pydoll.events.page import PageEvents
1514
from pydoll.mixins.find_elements import FindElementsMixin
1615
from pydoll.utils import decode_image_to_bytes
1716

@@ -134,12 +133,11 @@ async def go_to(self, url: str, timeout=300):
134133
Args:
135134
url (str): The URL to navigate to.
136135
"""
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+
143141
try:
144142
await self._wait_page_load(timeout=timeout)
145143
except asyncio.TimeoutError:
@@ -379,35 +377,13 @@ async def _wait_page_load(self, timeout: int = 300):
379377
"""
380378
Waits for the page to finish loading.
381379
"""
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

Comments
 (0)