TESTEVERYTHING

Showing posts with label Scrolling. Show all posts
Showing posts with label Scrolling. Show all posts

Sunday, 3 July 2016

Scroll the WebGrid using selenium webdriver

Hi,

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);

Thread.sleep(8000);


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

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

Thursday, 5 March 2015

Scrolling web page with Webdriver using java

We can scroll the web page using javaScript Executor or You can use the "org.openqa.selenium.interactions.Actions" class to move to an element:


Following classes you required in you java code
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement;
import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor;

1. We may require to scroll to bottom of the page and then perform operations. For this scenario we can use following code
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");

2. Some times, we may require to scroll to particular element and peform operations on that particular element. For this we need to pass the element on which we need to perform operation. For this scenario we ca use following code
WebElement element = driver.findElement(By.linkText("googleMap")); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
OR
WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();

3. We can also use the coordinates to scroll to particular position by passing the coordinates. For this scenario we use following code
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");

Which one is right ?

Translate







Tweet