How to handle this kind of scroll down and need to scroll up and down ?

How to handle this kind of scroll down and need to scroll up and down ?

Please provide the solution to handle the scroll down element.
we need to scroll down and up

Add Comment
  • 4 Answer(s)

    Hi Ravi,

    Its almost always not possible to answer a question without understanding the HTML structure of the element. Please try to give the HTML code also with the question. Same scroll behavior an be implemented in a variety of ways. One such way is using the scrollable properties of a Div element. I will try to answer your question based on the Div element that I can find, you can try to apply it for your element.
    UR: http://www.way2tutorial.com/html/example/div_tag_scrollbar_example.php

    In this URL there is a Div element which has a Vertical Scroll bar. To scroll that bar you have to look at two properties of the Scroll bar
    1. scrollHeight
    2. scrollTop

    scrollHeight gives you the total scroll-able length and scrollTop gives you the scroll position in the scroll at any given instance. Here is the code that modifies the scroll position. Try to run this and see how scrolling happens.

    
    WebDriver driver = DataProvider.BrowserFactory.getDriver("FirefoxDriver");
    
    driver.get("http://www.way2tutorial.com/html/example/div_tag_scrollbar_example.php");
    
    driver.manage().window().maximize();
    
    WebElement element = driver.findElement(By.cssSelector("#main > div:nth-child(9) > div"));
    
    Long height = (Long) ((JavascriptExecutor) driver).executeScript("return arguments[0].scrollHeight;", element);
    
    System.out.println("The height is " + height.toString());
    Long newScrollHeight = 0L;
    
    while(height > newScrollHeight)
    
    {
    
    ((JavascriptExecutor) driver).executeScript("return arguments[0].scrollTop = " + newScrollHeight.toString() + ";", element);
    
    newScrollHeight = newScrollHeight + 10;
    
    Thread.sleep(3000);
    
    

    Do let me know you need more explanation to this.

    Virender

    Professor Answered on May 16, 2015.

    Note that Thread.Sleep is not at all necessary. I have just put it to see the scrolling happening.

    on May 16, 2015.
    Add Comment

    Really Thanks. I will follow this scripting to deal with the scroll element.

    Train Answered on May 16, 2015.
    Add Comment

    Why this set of code is working with the expected scroll. Which code shall i share you.

    public void searchRole(String roleSearch)
    {

    WebElement scrollRole = driver.findElement(By.xpath(“//div[@id=’campaign-navigation’]/div[4]/div/a”));

    System.out.println(scrollRole.getTagName());

    Long height = (Long) ((JavascriptExecutor) driver).executeScript(“return arguments[0].scrollHeight;”, scrollRole);

    System.out.println(“The height is ” + height.toString());
    Long newScrollHeight = 0L;

    while(height > newScrollHeight)
    {
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    ((JavascriptExecutor) driver).executeScript(“return arguments[0].scrollTop = ” + newScrollHeight.toString() + “;”, scrollRole);

    newScrollHeight = newScrollHeight + 100;

    System.out.println(newScrollHeight);
    List<WebElement> roles = driver.findElements(By.xpath(“//span[@class=’campaign-name’]”));
    int total = roles.size();
    for(int i=0;i<total;i++){

    String expected = roles.get(i).getText();

    System.out.println(expected);

    }

    }

    Train Answered on May 16, 2015.

    I did not get your question, can you elaborate?

    Virender

    on May 16, 2015.

    your while block is empty and I am assuming that it will go in an infinite loop there, if you get height > 0.

    Virender

    on May 16, 2015.
    Add Comment

    Thanks Virendra

    Requirements :
    ——————
    I have to move the scroll down and up to search for a particular role in the above image. If role matches then pass otherwise fail.
    & an element XPATH is : //div[@id=’campaign-navigation’]/div[4]/div/a

    I have checked according to your code by using debug as well code is executing but scroll is not moving down and up.

    What would be the solution Please share ?

    Train Answered on May 17, 2015.

    You have to share the HTML code of the element and have to improvise on the solution in order to fit to your situation.

    Virender

    on May 17, 2015.
    Add Comment
  • Your Answer

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