Skip to content

feat: Adds wait_until to go_to #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

waqarmalik
Copy link

Adds wait_until option to tab's go_to methos to control when the navigation is considered complete.

The available wait_until options are:

'load' (default): Waits until the load event is fired. This event signifies that the entire page, including all dependent resources like stylesheets, scripts, iframes, and images, has finished loading.

'domcontentloaded': Waits until the DOMContentLoaded event is fired. This event occurs when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, or subframes to finish loading.

Pull Request

Description

Changes the current behavior of tab.go_to to not only wait for document.readyState . Instead allow users to specify the event they want to wait for. This PR adds an additional argument wait_until where the user can specify to wait until PageEvent.LOAD_EVENT_FIRED or PageEvent.DOM_CONTENT_EVENT_FIRED is fired.

Related Issue(s)

#194

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • [x ] New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes, no API changes)
  • Performance improvement
  • Tests (adding missing tests or correcting existing tests)
  • Build or CI/CD related changes

How Has This Been Tested?

Updated browser tests and ensured test pass with poetry run task test

Testing Checklist

  • [x ] Unit tests added/updated
  • [x ] Integration tests added/updated
  • All existing tests pass

Implementation Details

Created a asynccontextmaanger to handle pageload and dom event load pageevents. Quite similar to feature requested in #130

Making the wait_until public will address Issue 130 too.

API Changes

Additional optional wait_until input argument for tab.go_to
It accepts two options with the default been "load" to not change current behavior

  1. 'load': Waits until the load event is fired, indicating the page and all its resources have finished loading.
  2. 'domcontentloaded': Waits until the DOMContentLoaded event is fired, indicating the initial HTML document has been loaded and parsed.

Checklist before requesting a review

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have run poetry run task lint and fixed any issues
  • I have run poetry run task test and all tests pass
  • My commits follow the conventional commits style

Adds `wait_until` option to tab's `go_to` methos to control when the
navigation is considered complete.

The available wait_until options are:

'load' (default): Waits until the load event is fired. This event
signifies that the entire page, including all dependent resources like
stylesheets, scripts, iframes, and images, has finished loading.

'domcontentloaded': Waits until the DOMContentLoaded event is fired.
This event occurs when the initial HTML document has been completely
loaded and parsed, without waiting for stylesheets, images, or subframes
to finish loading.
Copy link

codecov bot commented Jul 12, 2025

Codecov Report

Attention: Patch coverage is 88.88889% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pydoll/browser/tab.py 88.88% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@@ -468,7 +468,7 @@ async def delete_all_cookies(self):
"""Delete all cookies from current browser context."""
return await self._execute_command(StorageCommands.clear_cookies(self._browser_context_id))

async def go_to(self, url: str, timeout: int = 300):
async def go_to(self, url: str, timeout: int = 300, wait_until: str = 'load'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the best approach. Some websites do not trigger these events.

In the original code, I use JavaScript to check the load status:

async def _wait_page_load(self, timeout: int = 300):
        """
        Wait for page to finish loading.

        Raises:
            asyncio.TimeoutError: If page doesn't load within timeout.
        """
        start_time = asyncio.get_event_loop().time()
        while True:
            response: EvaluateResponse = await self._execute_command(
                RuntimeCommands.evaluate(expression='document.readyState')
            )
            if response['result']['result']['value'] == 'complete':
                break
            if asyncio.get_event_loop().time() - start_time > timeout:
                raise WaitElementTimeout('Page load timed out')
            await asyncio.sleep(0.5)

This is more reliable. You can add a property to the Options object called load_strategy, with a default value of "complete". Then you'll have the possibility to set it to "interactive" and use it in the _wait_page_load method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants