Skip to content

Commit e76a364

Browse files
committed
BAEL-8376: How to Scroll an Element Into View in Selenium
1 parent fb8a1be commit e76a364

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung.scrollelementintoview</groupId>
6+
<artifactId>scrollelementintoview</artifactId>
7+
<packaging>jar</packaging>
8+
<version>1.0-SNAPSHOT</version>
9+
<name>scrollelementintoview</name>
10+
<url>http://maven.apache.org</url>
11+
12+
<properties>
13+
<maven.compiler.source>17</maven.compiler.source>
14+
<maven.compiler.target>17</maven.compiler.target>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
18+
<dependencies>
19+
<!-- Selenium Java -->
20+
<dependency>
21+
<groupId>org.seleniumhq.selenium</groupId>
22+
<artifactId>selenium-java</artifactId>
23+
<version>4.25.0</version>
24+
</dependency>
25+
26+
<!-- JUnit 5 -->
27+
<dependency>
28+
<groupId>org.junit.jupiter</groupId>
29+
<artifactId>junit-jupiter</artifactId>
30+
<version>5.10.0</version>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<!-- Compiler plugin -->
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-compiler-plugin</artifactId>
41+
<version>3.11.0</version>
42+
<configuration>
43+
<source>17</source>
44+
<target>17</target>
45+
</configuration>
46+
</plugin>
47+
48+
<!-- Update Surefire plugin to support JUnit 5 -->
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-surefire-plugin</artifactId>
52+
<version>3.2.5</version>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.baeldung.scrollelementintoview;
2+
3+
import org.openqa.selenium.*;
4+
import org.openqa.selenium.chrome.ChromeDriver;
5+
import org.openqa.selenium.JavascriptExecutor;
6+
import java.net.URL;
7+
8+
public class ScrollElementIntoView {
9+
10+
private WebDriver driver;
11+
12+
public void setUp() {
13+
driver = new ChromeDriver();
14+
driver.manage().window().maximize();
15+
16+
URL formUrl = getClass().getClassLoader().getResource("form.html");
17+
if (formUrl != null) {
18+
driver.get(formUrl.toString());
19+
} else {
20+
throw new RuntimeException("form.html not found in resources");
21+
}
22+
}
23+
24+
public void tearDown() {
25+
if (driver != null) {
26+
driver.quit();
27+
}
28+
}
29+
30+
public void scrollToElementCenter(WebElement element) {
31+
JavascriptExecutor js = (JavascriptExecutor) driver;
32+
js.executeScript(
33+
"const rect = arguments[0].getBoundingClientRect();" +
34+
"window.scrollBy({ top: rect.top + window.pageYOffset - (window.innerHeight / 2) + (rect.height / 2), behavior: 'smooth' });",
35+
element
36+
);
37+
}
38+
39+
public void runDemo() throws InterruptedException {
40+
WebElement firstName = driver.findElement(By.id("firstName"));
41+
WebElement middleName = driver.findElement(By.id("middleName"));
42+
WebElement lastName = driver.findElement(By.id("lastName"));
43+
44+
scrollToElementCenter(firstName);
45+
Thread.sleep(1000);
46+
firstName.sendKeys("John");
47+
48+
scrollToElementCenter(middleName);
49+
Thread.sleep(1000);
50+
middleName.sendKeys("William");
51+
52+
scrollToElementCenter(lastName);
53+
Thread.sleep(1000);
54+
lastName.sendKeys("Doe");
55+
56+
Thread.sleep(2000);
57+
}
58+
59+
public WebDriver getDriver() {
60+
return driver;
61+
}
62+
63+
public static void main(String[] args) {
64+
ScrollElementIntoView demo = new ScrollElementIntoView();
65+
try {
66+
demo.setUp();
67+
demo.runDemo();
68+
} catch (Exception e) {
69+
e.printStackTrace();
70+
} finally {
71+
demo.tearDown();
72+
}
73+
}
74+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Long Form Example</title>
6+
<style>
7+
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
8+
.container { width: 50%; margin: auto; padding: 20px; }
9+
label { margin-top: 20px; font-weight: bold; display: block; }
10+
input, button { margin-top: 5px; padding: 10px; font-size: 16px; }
11+
.spacer { height: 600px; } /* Simulate long form */
12+
</style>
13+
</head>
14+
<body>
15+
<div class="container">
16+
<h1>Long Form Example</h1>
17+
18+
<label for="firstName">First Name:</label>
19+
<input type="text" id="firstName" name="firstName">
20+
21+
<div class="spacer"></div>
22+
23+
<label for="middleName">Middle Name:</label>
24+
<input type="text" id="middleName" name="middleName">
25+
26+
<div class="spacer"></div>
27+
28+
<label for="lastName">Last Name:</label>
29+
<input type="text" id="lastName" name="lastName">
30+
31+
<div class="spacer"></div>
32+
33+
<button type="submit">Submit</button>
34+
</div>
35+
</body>
36+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.scrollelementintoview;
2+
3+
import org.junit.jupiter.api.*;
4+
import org.openqa.selenium.By;
5+
import org.openqa.selenium.WebDriver;
6+
import org.openqa.selenium.WebElement;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
10+
public class ScrollElementIntoViewUnitTest {
11+
12+
private ScrollElementIntoView helper;
13+
private WebDriver driver;
14+
15+
@BeforeAll
16+
void init() {
17+
helper = new ScrollElementIntoView();
18+
helper.setUp();
19+
driver = helper.getDriver();
20+
}
21+
22+
@AfterAll
23+
void tearDown() {
24+
helper.tearDown();
25+
}
26+
27+
@Test
28+
@DisplayName("Should scroll and fill First Name field")
29+
void givenFirstNameField_whenScrolledIntoView_thenFieldIsFilled() {
30+
WebElement firstName = driver.findElement(By.id("firstName"));
31+
helper.scrollToElementCenter(firstName);
32+
firstName.sendKeys("John");
33+
assertEquals("John", firstName.getAttribute("value"));
34+
}
35+
36+
@Test
37+
@DisplayName("Should scroll and fill Middle Name field")
38+
void givenMiddleNameField_whenScrolledIntoView_thenFieldIsFilled() {
39+
WebElement middleName = driver.findElement(By.id("middleName"));
40+
helper.scrollToElementCenter(middleName);
41+
middleName.sendKeys("William");
42+
assertEquals("William", middleName.getAttribute("value"));
43+
}
44+
45+
@Test
46+
@DisplayName("Should scroll and fill Last Name field")
47+
void givenLastNameField_whenScrolledIntoView_thenFieldIsFilled() {
48+
WebElement lastName = driver.findElement(By.id("lastName"));
49+
helper.scrollToElementCenter(lastName);
50+
lastName.sendKeys("Doe");
51+
assertEquals("Doe", lastName.getAttribute("value"));
52+
}
53+
}

0 commit comments

Comments
 (0)