

- 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 do you check scroll position using selenium?
We can check scroll position using Selenium. To check the position we shall use the Javascript executor. We have to verify the value of the window.pageYOffset in the browser.
While the URL is launched, the scroll is at the top the value of window.pageYOffset is 0. As we scroll to an element, the value of the window.pageYOffset shall be greater than 0.
Syntax
JavascriptExecutor j = (JavascriptExecutor) driver; Long v = (Long) j.executeScript("return window.pageYOffset;");
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class ScrollPosition{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/index.htm"); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // get current scroll position with Javascript Executor JavascriptExecutor j = (JavascriptExecutor) driver; Long v = (Long) j.executeScript("return window.pageYOffset;"); System.out.println("Scroll position after launch: " + v); // identify element WebElement n=driver.findElement(By.xpath("//*[text()='Careers']")); // Javascript executor to scroll to the element ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", n); Thread.sleep(200); // get current scroll position with Javascript Executor JavascriptExecutor js = (JavascriptExecutor) driver; double d = (double) js.executeScript("return window.pageYOffset;"); System.out.println("Scroll position after scrolling upto an element: "+d); driver.close(); } }
Output
- Related Questions & Answers
- How to find horizontal Scroll Position using jQuery?
- How to scroll down using Selenium WebDriver with Java?
- How to scroll to element with Selenium WebDriver using C#?
- How to scroll down a webpage in selenium using Java?
- How to set scroll left position in jQuery?
- How do you click on an element which is hidden using Selenium WebDriver?
- How to scroll a specific DIV using Selenium WebDriver with Java?
- How do you focus on new windows with selenium ide?
- How do you test pages that require authentication with Selenium?
- How to find left position of element in horizontal scroll container using jQuery?
- How can I scroll a web page using selenium webdriver in python?
- How to scroll up/down a page using Actions class in Selenium?
- Scroll Element into View with Selenium.
- How do you enter text in the edit box in Selenium?
- How do you get selenium to recognize that a page loaded?
Advertisements