Selenium Verify Scrollbar Presence Example with WebDriver
Though not much of common scenario, you may need to verify presence of scrollbar on a web page. You may come across this scenario more often if you are automating on mobile devices. This post covers a simple selenium verify scrollbar example. We have used Selenium WebDriver with Java code for this purpose.
Selenium WebDriver does not provide an out of the box API for solution of this problem. However, JavaScript can be used to detect present of scrollbar on a web page. Therefore, we will be executing JavaScript code using WebDriver’s JavaScriptExecutor API. JavaScript to detect scrollbar present is as follows.
1 |
return document.documentElement.scrollHeight>document.documentElement.clientHeight; |
In the example that follows, we will load Google home page and verify if scrollbar gets displayed in browser window. Follow below steps to implement Selenium verify scrollbar presence example.
- Since we need to implement JavaScriptExecutor in our test case, add below code in your test case method.
123String execScript = "return document.documentElement.scrollHeight>document.documentElement.clientHeight;";JavascriptExecutor scrollBarPresent = (JavascriptExecutor) driver;Boolean test = (Boolean) (scrollBarPresent.executeScript(execScript)); - The above code will return a Boolean variable. We will print a message in console as per whether its true or false. Add below code to implement this.
12345if (test == true) {System.out.print("Scrollbar is present.");} else if (test == false){System.out.print("Scrollbar is not present.");} - After performing above steps, your whole method should look similar to below code.
12345678910public void GoogleSearch() throws Exception {String execScript = "return document.documentElement.scrollHeight>document.documentElement.clientHeight;";JavascriptExecutor scrollBarPresent = (JavascriptExecutor) driver;Boolean test = (Boolean) (scrollBarPresent.executeScript(execScript));if (test == true) {System.out.print("Scrollbar is present.");} else if (test == false){System.out.print("Scrollbar is not present.");}} - Done. On executing the above method, a message about presence of scrollbar will be printed to console.
This is an example of how we can verify presence of scrollbar on web page. If you want to use scrollbar on web page, you can check out this tutorial.
We hope you find our Selenium Verify Scrollbar Presence Example useful in your test automation activities. Subscribe to our feed for more such useful tips in your inbox/feed.
Thanks for this piece of code. I am able to get the veritical scrollbar, but somehow its not working for horizontal scrollbar. Any idea what might be the problem?
Hi,
Try below JavaScript code for horizontal scrollbar.
return document.documentElement.scrollWidth>document.documentElement.clientWidth;
I have tried your code but in scroll-bar page I always get scroll-bar is not present. Just want to know how I am not sure what is this.
String execScript = “return document.documentElement.scrollHeight<document.documentElement.clientHeight;";
it is really getting right value from page where we have scroll-bar??
I want to down the scroll til down…
Can get help from you
driver(); should be initialize some where, where it should be and how it should be , please explain me.
in both case says “is present”. how to fix it