Skip to content
CodeKruPro

Scroll an element into view with JavaScript

Automation taskMediumAsked in: Accenture, Infosys, TCS

Interviewers ask: "How do you scroll to an element, or to the bottom of the page?" Calling scrollIntoView on the element is the usual answer. Selenium 4 also has a wheel action that scrolls to an element without JavaScript. Here you scroll a text field into view with JavaScript, check that it is displayed and print its value.

Scroll to Element with JavaScript

Scroll to Element

Use JavaScript to scroll an element into the viewport. This is essential when elements are not visible due to page scrolling.

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Find the preFilledTextField element
  3. Scroll it into view using JavaScript
  4. Verify it's displayed after scrolling
  5. Print its value

Expected output:

Scrolled to element: true
Value: Codekru