Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll a specific DIV using Selenium WebDriver with Java?

Some of WebElements are not recognized by WebDriver, WebDriver fails to find the element which is not visible in browser's visible area.

In order to make the WebElement viewable by WebDriver, We need to make that element to visible in browser's view to do scroll down on particular div!

How can I perform my action(scroll down and click) on particular area of my webpage. I tried lot, doesn't helped me.

Please help me resolve my issue.

like image 830
pandiaraj Avatar asked Nov 28 '14 12:11

pandiaraj


3 Answers

First of all, most of the answers posted here are just off the topic. I have been working with selenium 2 and selenium 3 now, the webdriver can handle window scroll to make an element visible.

For everyone posting snippets like:

driver.execute_script('scrollBy(0, 250)')

you do not get the question at all!

Actually I still did not find a way to properly simulate the drag action of scroll handle but this answer seems promising -- but I did not try.

So so far personally there are two ways to do this for me:

  1. Use Keys.ARROW_DOWN
  2. Use Keys.PAGE_DOWN

Actually there is a third way, just give up selenium and contact the website if they provide any API.

like image 153
Junchao Gu Avatar answered Oct 19 '22 18:10

Junchao Gu


The easiest way to do that is executing a Javascript to scroll the element up/down.

JavascriptExecutor jsExec = (JavascriptExecutor) driver;
jsExec.executeScript("document.getElementById('id').scrollDown += 100");
like image 10
Marcelo Xavier Avatar answered Oct 19 '22 17:10

Marcelo Xavier


driver.get("http://www.***.com/");
driver.manage().window().maximize();
WebElement scroll = driver.findElement(By.id("someId"));
scroll.sendKeys(Keys.PAGE_DOWN);
like image 5
Pavel Kononenko Avatar answered Oct 19 '22 16:10

Pavel Kononenko