

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver?
We can scroll a webpage using coordinates of a webelement in Selenium webdriver using the JavaScript Executor. Selenium executes JavaScript commands with the help of the executeScript method.
To get the unique coordinates of an element we shall create an object of the class Point which shall store the location of the webelement obtained from the getLocation method.
Then the individual x and y coordinate values can be computed from the getX and the getY methods respectively. Finally, to actually perform scroll upto the coordinates of an element, the command window.scrollBy(x coordinate, y coordinate) is passed as a parameter to the executeScript method.
Syntax
(JavascriptExecutor) driver).executeScript("window.scrollBy(100,150)");
here, 100 and 150 are the x and y coordinates respectively.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Point; import org.openqa.selenium.JavascriptExecutor; public class ScrollWithCodrdnts{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get("https://www.tutorialspoint.com/index.htm"); //identify element WebElement n = driver.findElement(By.linkText("Latest Courses")); //obtain element x, y coordinates with getLocation method Point p = n.getLocation(); int X = p.getX(); int Y = p.getY(); //scroll with Javascript Executor JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("window.scrollBy(" + X + ", " + Y + ");"); System.out.println("Text is: " + n.getText()); driver.quit(); } }
Output
- Related Questions & Answers
- How can I scroll a web page using selenium webdriver in python?
- How to Scroll Down or UP a Page in Selenium Webdriver?
- How to scroll down the page till page end in the Selenium WebDriver?
- How to scroll the Page up or down in Selenium WebDriver using java?
- How to scroll a specific DIV using Selenium WebDriver with Java?
- Get HTML Source of WebElement in Selenium WebDriver using Python.
- How to scroll up/down a page using Actions class in Selenium?
- How to scroll down using Selenium WebDriver with Java?
- How to scroll to element with Selenium WebDriver using C#?
- How to verify color of a web element in Selenium Webdriver?
- How to obtain the page title using Selenium webdriver?
- Save a Web Page with Python Selenium
- How to know the exact time to load a page using Selenium WebDriver?
- How to extract the text of a webelement in Selenium?
- How to get HTML code of a WebElement in Selenium?
Advertisements