5

I want to scroll down a page that is coming inside a form, not on the main page.

I tried this code:

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

enter image description here

The above code is not able to pick the scroll bar as it is not in the main page

  • I referred that but my question is different. In that question he is getting the scroll bar is in main page My scenario is Open website -> select a menu -> a form inside the main page will open and the scroll bar is inside that. screenshot of html -> prntscr.com/4ksrn0 – Som Ghosh Sep 8 '14 at 8:05
  • Did you try what I mentioned in the above my comment (if the scroll is in different page then move to that page or switch to particular window). – QA4it Sep 8 '14 at 11:41
  • yes, I tried that but the thing is findelement commands are working on that window without switching but the scroll is not working. Let me give you an example Go to www.flipkart.com then click on Trimmer link screenshot -> prntscr.com/4ku57a Then it will take you to the next page Now in the left side you can see the brand section and there is a scroll bar present there Screenshot -> prntscr.com/4ku5ke I want to operate that scroll bar Could you please give me code for this scenario – Som Ghosh Sep 8 '14 at 12:11
  • Are you trying to find a particular element by scrolling, or you just want to arbitrarily scroll down? If you want to scroll in order to view a specific element, you can use the scrollIntoView or scrollTop javascript methods. – Sam Woods Sep 9 '14 at 0:26
10

Please use below code.

driver.get("http://www.flipkart.com/");
driver.manage().window().maximize();
driver.findElement(By.linkText("Trimmer")).click();
WebElement scroll = driver.findElement(By.id("brand"));
scroll.sendKeys(Keys.PAGE_DOWN);
  • Thank you for your code. I tried the code but it gave me this error Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element (Session info: chrome=37.0.2062.103) Please re-check once – Som Ghosh Sep 8 '14 at 13:32
  • 1
    @Som Ghosh Try with FF and let me know. – Sachin Sep 8 '14 at 13:37
  • 1
    @Som Ghosh Please do not stick with one browser, verify with other browser also. If it is not work with one browser then it's different issue. – QA4it Sep 8 '14 at 13:44
  • @Sachin The problem is my product will only run in chrome due to some coding issues so as of now have to verify in chrome only – Som Ghosh Sep 8 '14 at 13:47
  • You can use the Actions class to do so without any issue in Chrome. Simply using key strokes doesn't work out. I too got the same exception of Chrome not being able to focus on the element. – demouser123 Jul 17 '15 at 16:46
2

I would suggest using the ActionChains (Actions in Java) class to do so. I tested the above code in both FF and Chrome and I am able to scroll down using the Keys.PAGE_DOWN keystroke.

    driver.get('http://flipkart.com/')

    driver.find_element_by_link_text('Bath Towels').click()



    WebDriverWait(driver,15).until(expected_conditions.presence_of_element_located((By.ID,"offertags")))


    act= ActionChains(driver)
    act.move_to_element(driver.find_element_by_id('offertags')).click()
    act.send_keys(Keys.PAGE_DOWN).perform()

I first used the click( ) method to click on the webelement and then sent the key stroke.

  • This I first used the click( ) method to click on the webelement and then sent the key stroke. is really the key here – George Pligoropoulos May 11 '19 at 11:11
1

I try the scroll down with loop, it's great with Python.

#!/usr/bin/python
# -*- coding: utf8 -*-

scroll_time = 13

...

scroll = driver.find_element_by_id("element_name")
for num in range(0, scroll_time):
    scroll.send_keys(Keys.PAGE_DOWN)

...
  • I believe he mentioned something with the find_element not working. I'm just not enough of a Selenium guy to follow the whole conversation. :-p – corsiKa Jul 16 '15 at 17:47
0

Something like this might help:

public static Object scrollElementIntoView(WebDriver driver, WebElement element) {
    return ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}

Where element is the object to which we want to scroll to.

0

Try the following code, it worked for me -

((JavascriptExecutor) d).executeScript("window.scrollTo(0, document.body.scrollHeight)");
  • This did not work for me. – MansoorShaikh Mar 29 '17 at 10:43
  • Can you post your code here which is used for scrolling? Make sure you have replaced the web driver obeject ('d') with your object. – Pankaj Pallewar Aug 28 '17 at 5:09

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