Wednesday, July 11, 2012

Selenium 2: How to scroll to element using WebDriver


Check new series of the articles. Review and user experience on test management systems. Functionality and usability.
-------------------

Some actions you take with WebElement's may fail due to those elements are not currently in the browser's visible area. So to make them able to interact you should first scroll vertically (or sometimes even horizontally :) ). However WebDriver does not provide explicit scrolling functionality.
Nevertheless you can bring the required element to the viewport. To to that just do the following

((Locatable)element).getLocationOnScreenOnceScrolledIntoView();

Where element is that WebElement you're going to interact with.

3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Have you gotten this to work on Firefox and IE?

    ReplyDelete
    Replies
    1. Certainly yes for firefox. Not for IE as I do not test it. If you cannot manage to make it work make sure you have latest version of selenium2 api. You have to keep it up to date especially for FireFox as it releases new version each new month.

      Delete