Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
March 20, 2024
Yes, you can scroll to the top of a webpage using JavaScript, similar to how you scroll to the bottom. To scroll to the top of the page, you can set the scroll position to 0 for both the horizontal and vertical scroll:
window.scrollTo(0, 0);
This line of code will scroll the webpage to the very top left corner (horizontal scroll position 0 and vertical scroll position 0).
If you’re using this in a Selenium WebDriver script in Java, you would execute this JavaScript command using the JavascriptExecutor interface like so:
Java import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; // other imports... public class ScrollToTopExample { public static void main(String[] args) { WebDriver driver = //... initialize your driver // ... your code to navigate to the webpage or perform actions // Scroll to the top of the page JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollTo(0, 0);"); // ... other actions or closing the driver } }