RE: Identify Scroll bar

Thanks Virendra for sharing the answer!!

Scroll Bar is getting handle by using offset method but still its getting an error i.e.
Exception in thread “main” org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Offset within element cannot be scrolled into view: (0, 42.5): [object XrayWrapper [object HTMLDivElement]]
Command duration or timeout: 10 milliseconds

&
few time it successfully gets executed

Could you provide the solution for it ?

Add Comment
9 Answers

Hi Ravi,
There is one more better way to scroll a list or any specific element on screen.

using  >>> org.openqa.selenium.internal.Locatable and
>>> org.openqa.selenium.interactions.internal.Coordinates

Try below code


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.interactions.internal.Coordinates;

import org.openqa.selenium.internal.Locatable;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

import org.testng.annotations.Test;
@Test

public class ajaxListScroll {
@Test

public void ajaxListScrollTest() throws InterruptedException{
// Instantiate driver with browser instance

driver.get("http://www.obout.com/ListBox/aspnet_ondemand_virtual_scroll.aspx");

WebDriverWait wait = new WebDriverWait(driver, 70);

wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_pageContent_PaperBox1_ListBox1_ob_CboListBox1MainContainer")));

String findScroll = driver.findElement(By.xpath("//div[2][@class='ob_iLboICFCM']/div/span")).getText();

//System.out.println("BFP noOfScroll :: " + findScroll);

String array[] = findScroll.split(" ");

//for (int i=0;i<array.length;i++){

// System.out.println("findScroll split :: " + i + " " + array[i]);

//}

array[5] = array[5].replace(".","");

int totalElementtoScroll = Integer.parseInt(array[5]);

//System.out.println("totalElementtoScroll :: " + totalElementtoScroll);
String array2[] = null;

int noOFElementonPageLoad=0;
for (int i=0;i<array.length;i++){

if (array[i].contains("-"))

array2 = array[i].split("-");

}

noOFElementonPageLoad = Integer.parseInt(array2[1]);

//System.out.println("noOFElementonPageLoad :: " + noOFElementonPageLoad);

int noOfTimetoScroll = totalElementtoScroll/noOFElementonPageLoad;

//System.out.println("noOfTimetoScroll :: " + noOfTimetoScroll);
for (int j=0; j<noOfTimetoScroll; j++){

String xpath = "//ul[@class='ob_iLboICBC']/li[" + noOFElementonPageLoad + "]/b";

//System.out.println("xpath passed :: " + xpath);

wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));

WebElement oggy = driver.findElement(By.xpath(xpath));

//System.out.println("oggy.getText() :: " + oggy.getText());

noOFElementonPageLoad += 25;

//System.out.println("noOFElementonPageLoad*2 :: " + noOFElementonPageLoad);

Thread.sleep(5000);

Coordinates coordinate = ((Locatable)oggy).getCoordinates();

coordinate.inViewPort();

}
System.out.println("Total Countries " + " :" + totalElementtoScroll);
for(int k=1;k<totalElementtoScroll+1;k++){

String printXpath = "//ul[@class='ob_iLboICBC']/li[" + k + "]/b";

//System.out.println("printXpath"+ printXpath);

System.out.println("Country " + k +" :"+ driver.findElement(By.xpath(printXpath)).getText());

}

//Thread.sleep(10000);

}
}

Supporter Answered on July 1, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.