2

I want to scroll down with my selected document. Tried the below code.

window.scrollTo(x, y);
const body = document.getElementsByClassName("body")[0];
body.scrollTo(x, y);

But, sometimes returns "undefined".

Edit 1: I got the solution. Provided the snippet below that helped me.

  1. To scroll hole page:

window.scrollTo(x, y)

  1. To scroll selected div (it should have a long area in order to be able to scrolled down):

document.getElementsByClassName("body")[0].scrollTo(x, y)

supputuri
  • 13,644
  • 2
  • 21
  • 39
Raqun Bob
  • 156
  • 1
  • 12
  • 1
    Thank you. Also, you should use the `querySelector` document method instead of `getElementsByClassName`. It is more versatile. Check it out here: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector – Alexis Philip Jul 26 '19 at 08:57
  • pls separate your question and your answer and post a real answer – Synoon Jul 26 '19 at 08:58
  • Read this: [scroll element into view with selenium](https://stackoverflow.com/questions/3401343/scroll-element-into-view-with-selenium) – Dars Jul 26 '19 at 12:01

1 Answers1

1

here is code to scroll till particular element in DOM.

C#

var element = driver.FindElement(By.XPath(xpath));
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].scrollIntoView(true);", element);

Java

var element = driver.FindElement(By.XPath(xpath));
JavaScriptExecutor js = (JavaScriptExecutor)driver;
js.executeScript("arguments[0].scrollIntoView(true);", element);

let me know if this doesn't resolve your issue. am happy to help further

Krunal Patel
  • 257
  • 3
  • 10