1

I need to select a image which appears on scrolling down a page. Set of images loads on hitting bottom of page and further scrolling down loads another set of images.

I tried using scroll(), scrollBy() and Actions method but none is responding. But using scrollIntoView() method alone scrolls the page. since the image i needed to select appears only after loading on scroll is completed, i couldn't use scrollIntoView() here . Please note scroll window im trying is inside iframe and div tag.

((JavascriptExecutor) driver).executeScript("scroll(0,400)");

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

Actions action = new Actions(driver);
action.sendKeys(Keys.PAGE_DOWN);
2
  • have u tried by scroll to element position? Commented Nov 17, 2016 at 14:17
  • Element is visible only after scroll. so couldn't use scroll to. Commented Nov 18, 2016 at 12:59

1 Answer 1

1

To scroll to the bottom, set the scrollTop property to a maximum. The container having the scrollbar is usually the <body>:

((JavascriptExecutor)driver).executeScript(
    "document.body.scrollTop = -1 >>> 1");

or the <html>:

((JavascriptExecutor)driver).executeScript(
    "document.documentElement.scrollTop = -1 >>> 1");

or scrollable <div style="overflow: scroll;"> :

WebElement div = driver.findElement(By.cssSelector(...));
((JavascriptExecutor)driver).executeScript(
    "arguments[0].scrollTop = -1 >>> 1", div);

Note that you first need to set the context on the targeted frame.

Sign up to request clarification or add additional context in comments.

11 Comments

i tried both the code but didn't help. So do we need to set the container somewhere ?. I have switched to iframe and trying this code which didn't scroll for me,
Which element has the scrollbar ?
I'm not sure how to find the element which has scrollbar. Just using inspect element found scroll bar exist inside one of div tag. please let me know how to find it
The highlighted element should match the size of the scrollbar when it is inspected. Then simply set the scrollTop property on this element with executeScript.
Yes. Scroll works fine in console and as well via code ((JavascriptExecutor)driver).executeScript( "arguments[0].scrollTop = -1 >>> 1", element);. There was issue in loading the contents. So handling it worked for me. Thanks :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.