How to do Scrolling on a webpage using Selenium Csharp

asked 10 months ago

Sometimes the webelement might not appear on the webpage eye view, selenium webdriver interact with the webelement which they appear in the webpage. To interact with such elements we need to scroll up, down, horizontal and vertical movement. To do scrolling on the web page, there are few methods available in the selenium webdriver.

Navigate to https://www.qafeast.com/demo  , click the Scrolling tab.

Scrolling in web page Selenium Csharp

The submit button is at the bottom of the page, selenium webdriver mimics as a user to click the control. The submit button is not visible to the user, if selenium scripts are written to click the button then it will throw an error "Element not visible".In order to click the submit button, we can make the scroll at the end of the page, and the button will be visible to click. 

 

Scroll to the element:
 

IWebElement scrollToELement = driver.FindElement(By.XPath("//label[text()='Scrolling']"));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", scrollToELement);
 

 

Scroll to the bottom of the page

IJavaScriptExecutor js = ((IJavaScriptExecutor)driver);
js.ExecuteScript("window.scrollTo(0, document.body.scrollHeight)");


Scroll down to some extent

IJavaScriptExecutor js = ((IJavaScriptExecutor)driver);
js.ExecuteScript("window.scrollTo(0, 250)");


Scroll to top of the page

IJavaScriptExecutor js = ((IJavaScriptExecutor)driver);
js.ExecuteScript("window.scrollTo(document.body.scrollHeight, 0)");


Scroll up to some extent

IJavaScriptExecutor js1= ((IJavaScriptExecutor)driver);  js1.ExecuteScript("window.scrollTo(0, -250)");
js1.ExecuteScript("window.scrollTo(0, -250)");

 

Categories