How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver

0 votes
How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver ?
Jul 5, 2019 in Selenium by Piyush
5,679 views

2 answers to this question.

0 votes

Hi Piyush, if you want to scroll a webpage using coordinates of an element, then you first need to locate the element and find its location. Then from location, you can get the X and Y coordinates, which you can pass in scrollBy method:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.Point;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;



public class ScrollTest {

    WebDriver driver;

    @BeforeTest

    public void setUp() {

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anvi_R\\chromedriver.exe");

        driver = new ChromeDriver();

    }

    @Test

    public void test01() throws InterruptedException {

        driver.get("https://www.edureka.co");

        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        WebElement viewAllCoursesBtn = driver.findElement(By.xpath("//section[5]//article[1]//div[1]//span[2]//a[1]"));

        Point point = viewAllCoursesBtn.getLocation();

        int x_coordinate = point.getX();

        int y_coordinate = point.getY();

        scrollToElement(x_coordinate, y_coordinate);

    }

    public void scrollToElement(int x, int y) {

        JavascriptExecutor javScriptExecutor = (JavascriptExecutor) driver;

        javScriptExecutor.executeScript("window.scrollBy(" + x + ", " + y + ");");

    }

}
answered Jul 5, 2019 by Anvi
• 14,150 points
0 votes
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy("X", " y ")");
answered Aug 31, 2020 by Sri
• 3,190 points

Related Questions In Selenium

0 votes
2 answers

How to scroll the Page up or down in Selenium WebDriver? (using java)

JavascriptExecutor jsx = (JavascriptExecutor)driver; jsx.executeScript("window.scrollBy(0,555)", ""); or Action classes ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
18,431 views
0 votes
2 answers
0 votes
8 answers

How to open a link in new tab of chrome browser using Selenium WebDriver?

This below code works for me in ...READ MORE

answered Dec 14, 2020 in Selenium by Gitika
• 65,910 points
101,347 views
0 votes
1 answer

How can I scroll a web page in Mozilla Firefox using Selenium?

Hi Rohan, steps to scroll a webpage in ...READ MORE

answered May 14, 2019 in Selenium by Pratibha
• 3,690 points
2,852 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,576 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,557 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,603 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,506 views
0 votes
1 answer

How to scroll up/down a page using Actions class in Selenium?

Hi Naetik, you can use Actions class ...READ MORE

answered Jul 5, 2019 in Selenium by Anvi
• 14,150 points
19,849 views
0 votes
1 answer

How to extract text from a web page using selenium and save it as a text file?

Hello Isha, you can checkout this code ...READ MORE

answered May 7, 2019 in Selenium by Anvi
• 14,150 points
33,088 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP