Page Scroll using Selenium webdriver

Source: Internet
Author: User
Tags gety xpath

Using JavaScript


Scroll down:

Import Org.openqa.selenium.JavascriptExecutor;
Webdriver Driver = new Firefoxdriver ();
Javascriptexecutor JSE = (javascriptexecutor) driver;
Jse.executescript ("Scroll (0, 250)"); Y value ' + ' can be altered

Scroll up:

Javascriptexecutor JSE = (javascriptexecutor) driver;
Jse.executescript ("Scroll (250, 0)"); X value ' + ' can be altered

Scroll Bottom of the Page:

Javascriptexecutor JSE = (javascriptexecutor) driver;
Jse.executescript ("Window.scrollto (0,math.max (document.documentelement.scrollheight,document.body.scrollheight , document.documentElement.clientHeight)); ");
(OR)
actions = new actions (driver);
Actions.keydown (Keys.control). SendKeys (Keys.end). Perform ();

Full Scroll to bottom in slow motion:

for (int second = 0;; second++) {
if (second >=60) {
Break
}
((Javascriptexecutor) driver). Executescript ("Window.scrollby (0,400)", ""); Y value ' x ' can be altered
Thread.Sleep (3000);
}
(OR)
Javascriptexecutor JSE = (javascriptexecutor) driver;
for (int second = 0;; second++) {
if (second >=60) {
Break
}
Jse.executescript ("Window.scrollby (0,800)", ""); Y value ' + ' can be altered
Thread.Sleep (3000);
}

Scroll automatically to your webelement:

Point Hoveritem =driver.findelement (By.xpath ("Value")). GetLocation ();
((Javascriptexecutor) driver). Executescript ("return window.title;");
Thread.Sleep (6000);
((Javascriptexecutor) driver). Executescript ("Window.scrollby (0," + (Hoveritem.gety ()) + ");
Adjust your Page view by making changes right over here (Hoveritem.gety ()-400)
(OR)
((Javascriptexecutor) driver). Executescript ("Arguments[0].scrollintoview ();", Driver.findelement (By.xpath ("Value ‘)]")));
(OR)
webelement element = Driver.findelement (By.xpath ("Value"));
coordinates coordinate = ((locatable) Element). Getcoordinates ();
Coordinate.onpage ();
Coordinate.inviewport ();


Using KeyBoard



We have both options for scrolling in Web page. Using Actions Class
Package name:org.openqa.selenium.interactions.Actions Java code: ctrl+end | Scroll to Bottom of the page actions = new actions (driver);
Actions.keydown (Keys.control). SendKeys (Keys.end). Perform ();
Without Using Actions Class Java code:
For (int i=0;i<10;i++)                     { driver.findelement (By.tagname ("Body")). SendKeys (Keys. down);                     }  

Page Scroll using Selenium webdriver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.