简体   繁体   中英

How to implement scroll on WebElement on Selenium in Java?

I have a scroll that is responsible to scroll a table on the page, not the whole page, so I cant use window.scrollBy

I've tried to find the scroll's container (I don't see a scroll specific object in the DOM) and use the following code:

JavascriptExecutor jse =  (JavascriptExecutor)driver;

jse.executeScript("arguments[0].scrollBy(0,200);",scrollContainer);

(ScrollContainer is WebElement)

I'm getting the exception:

org.openqa.selenium.WebDriverException: unknown error: arguments[0].scrollBy is not a function

What am I doing wrong?

Thanks

The method HTMLElement.scrollBy is not supported by all the browsers. Use scrollLeft or scrollTop instead:

JavascriptExecutor jse =  (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollTop += 200;", scrollContainer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM