question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

scrollIntoView() not working as expected. Acts as if get() not returning element. Same code works with click()

See original GitHub issue

Current 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
sam3kcommented, Apr 2, 2020

I was having, what seems to be the same or very similar issue, where neither Cypress’ cy.get('.some-selector').scrollIntoView() nor JavaScript’s cy.get('.some-selector').then(el => el[0].scrollIntoView()) worked. It all came down to me having the following CSS:

html { scroll-behavior: smooth; }

If I remove this, everything works. Hopefully this helps some folks.

5reactions
citypaulcommented, Oct 26, 2017

The following does pass:

const footballPage = 'http://www.test.bbc.co.uk/sport/beta/live/football/23031537';

describe('Get Involved', () => {
  it('Does not do much!', () => {
    cy
      .visitAndPoll(footballPage)
      .get('article#post_58d3a03ee4b0e1456fe22080')
      .then(el => {
        el[0].scrollIntoView();
        return el;
      })
      .should('be.visible');
  });
});

However, I feel like the then() should not be necessary here. I understand the get returns a collection of elements, but even chaining with .last() or .eq(0) does not work.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found