playwright
playwright copied to clipboard
[Feature]: Proper support for scrolling to the bottom of a page
🚀 Feature Request
I'd like a simple method to allow me to scroll my browser page to the bottom. Something like
page.scroll_to_bottom()
Or maybe
page.scroll_to('bottom')
Where the possible answers are top and bottom.
Example
No response
Motivation
The motivation here is that scrolling to the bottom of a page is a common way to trigger infinite scrolling pages to load more content.
It's easy to search and find people working around the lack of this feature via Javascript. In fact, a Playwright developer suggests using Javascript in https://github.com/microsoft/playwright-python/issues/683
However, I'd argue that infinite scrolling pages are common enough that this is a worthwhile feature to add to Playwright.
This feature is also discussed in https://github.com/microsoft/playwright/issues/1115 - however the issue was closed because mouse wheel interaction was implemented, but that doesn't solve the infinite scroll scenario.
Should this feature request be raised in https://github.com/microsoft/playwright rather than the Python specific repository?
@SamStephens This feature is not that straightforward. It's impossible to scroll to the bottom of an "infinite list" pattern. On many pages, instead of scrolling the document, you have to scroll some particular scrollable container inside. On other pages, you have to scroll an iframe instead. There are probably many more edge cases.
Given the limitations, we recommend:
- Either
evaluate("window.scrollBy(0, 100000)"if you know that scrolling the document is sufficient. - Or call
locator.scroll_into_view_if_needed()with a locator somewhere at the bottom of the page. Most likely, that's actually what you need - to scroll up to some point.
Let me know whether the above methods work for you.
I'm currently using evaluate("window.scrollBy(0, 100000)"), and that's the method I see most people using online.
I guess my thinking is why not wrap up evaluate("window.scrollBy(0, 100000)") in a method with a semantically meaningful name, if it's something a number of users have used, something with enough utility you and others recommend it to people.
I guess my thinking is why not wrap up
evaluate("window.scrollBy(0, 100000)")in a method with a semantically meaningful name, if it's something a number of users have used, something with enough utility you and others recommend it to people.
Introducing an API requires a certain quality of the implementation. Due to the limitations I listed, we are not confident that we can implement such an API in a robust manner, unless it's called page.scrollDocumentScrollingElementToTheBottomMayBeFlaky() or similar. I'd really recommend scrolling some kind of a footer into view instead.
@dgozman that's fair. I wonder if it's worth putting a little something in the documentation about scrolling practices, to pre-empt people with similar questions and problems to me. If not, I'm happy to close this issue; thanks for your explanations.
Thanks for doing that @dgozman, I'm guessing you don't want to explicitly document evaluate("window.scrollBy(0, 100000)").
Thanks for doing that @dgozman, I'm guessing you don't want to explicitly document
evaluate("window.scrollBy(0, 100000)").
Yep, we think that other methods are better and easier to explain.