If you are trying to write a functional test with the Selenium webdriver, you might occasionally run into this error :-

Error: Failed to execute clickElement({"ELEMENT":"2"}): Error response status: 3
4. Element cannot be scrolled into view:javascript:void(0)
Command duration or timeout: 33 milliseconds

This is happening because the webdriver is trying to scroll the element into view before it clicks it and the browser window size may not be big enough for it to view the element.

You have 2 options :-

  • If you have a 2nd monitor attached, you can make it the default monitor and run the tests there and hopefully, this should resolve the issue. Although, this is not ideal.
  • In your code, you can tell the webdriver to increase the window size before it does any assertions or clicks.
    return browser
        .get(require.toUrl('http://localhost:7080/myapp'))
        .setWindowSize(1200, 1000)
        .waitForElementByCssSelector('.container', 1000)
        .title()
        .then(function (text) {
            expect(text).to.equal('My title');
        })
        .end();

    One thing to note here is that, this may not work in Chrome (works in Firefox though), so it’s better to use the maximize() function which works in both Chrome and Firefox.

    return browser
        .get(require.toUrl('http://localhost:7080/myapp'))
        .maximize('current') // 'current' is the default window handle
        .waitForElementByCssSelector('.container', 1000)
        .title()
        .then(function (text) {
            expect(text).to.equal('My title');
        })
        .end();

I am using Intern in the above example which uses wd.js which is a Node client for Webdriver but the code above should work for any testing framework that uses Webdriver, although you might want to make sure the syntax for maximize() is the same.

Tagged with:  

2 Responses to Selenium webdriver “Element cannot be scrolled into view”

  1. Ilyas Patel says:

    Yeah it is useful to maximise the window before running each test. I find with Chrome that I need to have a JavaScript method which scrolls to a given element so it is in view. This is normal when the element is below the fold.

    • Anuj Gakhar says:

      Hi IIyas, I haven’t yet encountered a scenario where the element is below the fold when page is maximised, but I guess you can also use the execute() or executeScript() function of the wd.js/webdriver.

Leave a Reply

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 292 other subscribers

© 2011 Anuj Gakhar
%d bloggers like this: