scrollIntoView() not working as expected. Acts as if get() not returning element. Same code works with click()
See original GitHub issueCurrent behavior:
scrollIntoView() not working. Produces the error: SyntaxError: Failed to execute 'querySelector' on 'Document': '0' is not a valid selector.
despite the fact that the element in question is available to cypress.
If I swap out the scrollIntoView()
method for click()
instead, the test passes and you can see the page actually scrolls to the element.
Desired behavior:
Should scroll element into view, and the test should pass.
How to reproduce:
I’ve produced a repository that demonstrates the issue here:
https://github.com/citypaul/cypress-scrollintoview
Test code:
This test fails as if it cannot find the element in question:
const footballPage = 'http://www.test.bbc.co.uk/sport/beta/live/football/23031537';
describe('Get Involved', () => {
it('Does not do much!', () => {
cy.visit(footballPage);
cy
.get('#post_58d3a03ee4b0e1456fe22080')
.scrollIntoView()
.should('be.visible');
});
});
However, this test, also looking for the same element, does pass. Just using click()
instead of scrollIntoView()
:
const footballPage = 'http://www.test.bbc.co.uk/sport/beta/live/football/23031537';
describe('Get Involved', () => {
it('Does not do much!', () => {
cy.visit(footballPage);
cy
.get('#post_58d3a03ee4b0e1456fe22080')
.click()
.should('be.visible');
});
});
Additional Info (images, stack traces, etc)
cypress_runner.js:136339 SyntaxError: Failed to execute 'querySelector' on 'Document': '0' is not a valid selector.
Error: Failed to execute 'querySelector' on 'Document': '0' is not a valid selector.
at a.scrollTo (http://www.test.bbc.co.uk/sport/beta/live/football/23031537:310:1746)
From previous event:
at runCommand (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:61508:14)
at next (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:61590:14)
From previous event:
at next (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:61590:34)
at <anonymous>
From previous event:
at http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:61609:37
From previous event:
at run (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:61607:15)
at Object.cy.(anonymous function) [as visit] (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:61827:11)
at Context.runnable.fn (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:61958:20)
at callFn (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:31825:21)
at Test.Runnable.run (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:31818:7)
at http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:64801:28
From previous event:
at Object.onRunnableRun (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:64800:20)
at $Cypress.action (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:60312:51)
at Test.Runnable.run (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:63951:20)
at Runner.runTest (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:32288:10)
at http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:32394:12
at next (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:32208:14)
at http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:32218:7
at next (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:32150:14)
at http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:32186:5
at timeslice (http://www.test.bbc.co.uk/__cypress/runner/cypress_runner.js:27427:27)
logError @ cypress_runner.js:136339
- Operating System: Mac OSX (10.12.3 (16D32))
- Cypress Version: 1.0.2
- Browser Version: Chrome 61
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Why even though there is no error, scrollintoview doesn't want ...
If the element is not visible on the page, scrollIntoView will not work as expected. Make sure that the scrollIntoView function is being ......
Read more >scrollIntoView - Cypress Documentation
Scroll an element into view. It is unsafe to chain further commands that rely on the subject after .scrollIntoView(). Syntax Usage Correct Usage....
Read more >Element.scrollIntoView() - Web APIs | MDN
The Element interface's scrollIntoView() method scrolls the element's ancestor containers such that the element on which scrollIntoView() is ...
Read more >.scrollIntoView() | TestController | Test API | API | Docs
When TestCafe interacts with an off-screen DOM element, it scrolls that element into view. There is usually no need to use the scroll...
Read more >Scroll Events and Intersection Observer - Beginner JavaScript
function scrollToAccept() { const terms = document. ... Either of them work in this case because a scroll event does not bubble like...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I was having, what seems to be the same or very similar issue, where neither Cypress’
cy.get('.some-selector').scrollIntoView()
nor JavaScript’scy.get('.some-selector').then(el => el[0].scrollIntoView())
worked. It all came down to me having the following CSS:If I remove this, everything works. Hopefully this helps some folks.
The following does pass:
However, I feel like the
then()
should not be necessary here. I understand theget
returns a collection of elements, but even chaining with.last()
or.eq(0)
does not work.