Scrolling using Selenium WebDriver with Java

前端 未结 8 2087
长情又很酷
长情又很酷 2020-12-25 08:47

I am using Selenium WebDriver to automate my browser tests. My browser header is floating and is always present irrespective of the browser scroll.

<
8条回答
  •  情歌与酒
    2020-12-25 09:47

    Use below code for scrolling up and scrolling down

    Actions dragger = new Actions(driver);
    
    WebElement draggablePartOfScrollbar = driver.findElement(By.xpath(""));
    
    // drag downwards
    
    int numberOfPixelsToDragTheScrollbarDown = 50;
    
    for (int i=10 ; i<500 ; i=i+numberOfPixelsToDragTheScrollbarDown) {
        try {
            // this causes a gradual drag of the scroll bar, 10 units at a time
            dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
            Thread.sleep(1000L);
        } catch(Exception e1){}
    } 
    
    // now drag opposite way (downwards)
    numberOfPixelsToDragTheScrollbarDown = -50;
    for (int i=500;i>10;i=i+numberOfPixelsToDragTheScrollbarDown){
        // this causes a gradual drag of the scroll bar, -10 units at a time
        dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
        Thread.sleep(1000L);
    }
    

提交回复
热议问题