Sunday, 3 July 2016

Scroll the WebGrid using selenium webdriver


Sometimes in webpage there is web table grid present on page and we need to scroll the grid to access the element information. Because of asynchronous call data will not load completely until unless you scroll the grid.

From wiki..

Synchronous means that you call a web service (or function or whatever) and wait until it returns - all other code execution and user interaction is stopped until the call returns. Asynchronous means that you do not halt all other operations while waiting for the web service call to return
Here I have written the method that will scroll your grid until it reach to end of data row.

Note: ElementXpath will be your data table rows xpath
like  ElementXpath = "//table//tr";

public static void ScrollView(WebDriver driver,String ElementXpath) throws

Exception {

int elemntCountBeforeScroll= driver.findElements(By.xpath(ElementXpath)).size();

if (elemntCountBeforeScroll>0){

WebElement element = driver.findElements(By.xpath(ElementXpath)).get(elemntCountBeforeScroll-1);

JavascriptExecutor jse = (JavascriptExecutor) driver;

jse.executeScript("arguments[0].scrollIntoView(true);", element);


int elemntCountAfterScroll= driver.findElements(By.xpath(ElementXpath)).size();

for (int i = 0; i < elemntCountBeforeScroll; i++) {
{ elemntCountBeforeScroll = elemntCountAfterScroll;
 element = driver.findElements(By .xpath(ElementXpath)).get(elemntCountAfterScroll-1);
 jse = (JavascriptExecutor) driver;
 jse.executeScript("arguments[0].scrollIntoView(true);", element); 
elemntCountAfterScroll = driver.findElements(By .xpath(ElementXpath)).size(); }

No comments:

Post a Comment

Which one is right ?