1

I am working for a website automation testing. I need to scroll many times. How to find a Scroll bars`s Xpath in a browser ?

  • You do not need scroll bar's xpath, there are functions can do scrolling out of box for you, google them – Yu Zhang Feb 18 '17 at 11:06
  • I tried many but I need scroll bar`s xpath. Is it possible to have it ? – Gaurav Feb 18 '17 at 11:15
  • Why do you need to scroll? To get an element in view? What are you trying to test? With some more context we could give a better answer. – Niels van Reijmersdal Feb 18 '17 at 11:24
2

You can't. Scrollbars are part of the browser and not the HTML document. This means you cannot control them unless it isn't a real scrollbar (one that is created with HTML+JavaScript).

ScrollBars are operating system elements and cannot be driven by Selenium or WebDriver like other elements.

Possible ways to scroll with Selenium are:

Scroll with JavaScript

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");

This scrolls down 250 pixels for more idea's check this question: https://stackoverflow.com/questions/12293158/page-scroll-up-or-down-in-selenium-webdriver-selenium-2-using-java

Scroll to a known element with Actions

WebElement element = driver.findElement(By.id("my-id")); Actions
actions = new Actions(driver); actions.moveToElement(element);
actions.perform();

This scrolls to the element, for more idea's for scrolling to elements check this question: https://stackoverflow.com/questions/3401343/scroll-element-into-view-with-selenium

I think the Action way is preferred, but it needs an element.

Trigger the real scrollbar

If for some reason you really need to use the scrollbar like a real user would, because you have some action/event/trigger (I cannot come up with a good example). Then you could have a look at automation frameworks that can influence the operating system for example like: Sikuli or AutoIt

This should be your last resort though, I would suspect the window.scrollBy() to fire most events you would like to test.

0

As noted by others, scrollbars are part of the browser and not the HTML document.

But you can access the scrollbars by using visual web testing tools like Kantu or Sikuli. The Kantu Browser is is a wrapper for Chromium and thus has access to the scrollbar via its SCROLL page up/down command (pretty obvious command name).

Sikuli even has commands for things like scroll-wheel simulation (wheel(target, WHEEL_UP | WHEEL_DOWN, steps).

0

This code is for scrolling to the full height of the page. It will scroll down to the end of the page.

 String jsCode1 = "window.scrollBy(0, document.body.scrollHeight)";
 JavascriptExecutor je = (JavascriptExecutor)driver;
 je.executeScript(jsCode1);      

This is for scrolling in upward direction.

 String jsCode2 = "window.scrollBy(0, -1000)";
 je.executeScript(jsCode2);  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.