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 WebEle
As you found, webdriver won't find elements that aren't visible so you need to scroll to the element. However you can't scroll directly to the element because webdriver won't find it until it is visible. Catch-22.
However I do have a way that you can scroll within a DIV. First assign the DIV you want to scroll to an element variable, and then you can use javascript to scroll that element instead of scrolling the entire window.
Some VB.NET example code that should be easily adapted to other languages:
Dim div_to_scroll As IWebElement = driver.FindElement(By.XPath("[put your xpath here]"))
driver.ExecuteJavaScript("arguments[0].scrollBy(0,500)", div_to_scroll)
' Short pause to make sure the screen updates:
System.Threading.Thread.Sleep(500)
If you don't know how far you need to scroll then you will need a loop with a test to see if the desired element is visible yet. This isn't a particularly elegant or fast way of doing things but webdriver isn't a fast solution to begin with and I have found this method to be very robust.
None of the posted answers worked for me, however I have found a solution based on this post.
((JavascriptExecutor) driver).executeScript(
"arguments[0].scrollTop=arguments[1].offsetTop",
divWithScrollbarElement,
elementToScrollTo);
where divWithScrollbarElement
is the div element which you are looking to scroll, and elementToScrollTo
is the child element which you want to make viewable (which in my case was actually the parent of the element which I was initially trying to view). If elementToScrollTo
is not actually in the DOM yet, you may need to use the script once to scroll down as far as possible, and then again once more elements have loaded.
First you should do scroll rather than find element so do like below :
document.getElementById("your div id").scrollTop(250);
After above you can find that specific div.
You can also try below :
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("javascript:window.scrollBy(250,350)");
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:
Keys.ARROW_DOWN
Keys.PAGE_DOWN
Actually there is a third way, just give up selenium and contact the website if they provide any API.