[Selenium]Turn Page By Scroll Bar

本文介绍了一种通过操作滚动条实现页面分页,并在当前页面中查找特定元素的方法。主要内容包括:计算滚动条每次移动的高度、确定总页数及不足整页的情况,以及如何根据页数进行翻页。

Description:

Need to turn page by operating scroll bar and find out the element in the current page.

Previous page will not exist in DOM structure when turning page.

Solution:

  1. Get the total height, button height, scroll bar height
  2. Calculate total move height, totalMoveHeight = totalHeight - 2*buttonHeight - scrollbarHeight
  3. Calculate page number, pageNum =  totalMoveHeight/scrollbarHeight
  4. Calculate the last page when it is less then a whole page, lessThenOnePageHeight = totalMoveHeight%scrollbarHeight
  5. Turn page according to the page number and lessThenOnePageHeight

Code:

/**************************Report Portal–>ReportProductionFlow.java******************************/

public void seleteTemplate_NotClassifiedFactsheet(String template){

        //Scroll the scroll bar page by page

        Actions actions = new Actions(page.getDriver());

        int totalHeight = page.getDiv_scrollbar_TemplateMappingSetting().getSize().getHeight();

        int buttonHeight = page.getButton_ScrollbarDown().getSize().getHeight();

        int scrollbarHeight = page.getScrollbar_TemplateMappingSetting().getSize().getHeight();

        int totalMoveHeight = totalHeight - buttonHeight - buttonHeight - scrollbarHeight;

        int pageNum = totalMoveHeight/scrollbarHeight;

        int lessThenOnePageHeight = totalMoveHeight%scrollbarHeight;

        if(lessThenOnePageHeight>0){

            pageNum+=1;

        }

        for(int i=0;i<pageNum;i++){

            if ((i==(pageNum-1))&&(lessThenOnePageHeight>0)) {

                scrollbarHeight=lessThenOnePageHeight;

            }

            actions.dragAndDropBy(page.getScrollbar_TemplateMappingSetting(), 0, scrollbarHeight).perform();

            SeleniumUtil.sleep(1);

            List <WebElement> groupList = page.getGroupListInTemplateMapping();

            int groupNum = groupList.size();

            for(int j=0;j<groupNum;j++){

                WebElement groupEl=groupList.get(j);

                String groupName = groupEl.getText();

                

                if(groupName.equals("Not Classified")){

                    System.out.println("Find Group : "+groupName+" in page "+i);

                    WebElement factsheetTemplateEl=page.getDDL_NotClassifiedFactsheet();

                    factsheetTemplateEl.click();

                    page.getLink_Template(template).click();

                }

            }

        }

    }

/**************************Report Portal–>ReportProductionFlow.java******************************/

 

/**************************Report Portal–>ReportProductionPage.java******************************/

public WebElement getDiv_scrollbar_TemplateMappingSetting(){

        return SeleniumUtil.waitForElementPresent(driver, By.cssSelector("div#uidialog7.uidialog div.uidialogcontent div#mapEditDlgDiv div.tempmapsetup div.itemsgrid div.rtq-grid div.rtq-grid-sz div.rtq-scrollpanel div.rtq-scrollbar.rtq-scrollbar-y"));

}

public WebElement getButton_ScrollbarDown(){

        return SeleniumUtil.waitForElementPresent(driver, By.cssSelector("div#uidialog7.uidialog div.uidialogcontent div#mapEditDlgDiv div.tempmapsetup div.itemsgrid div.rtq-grid div.rtq-grid-sz div.rtq-scrollpanel div.rtq-scrollbar.rtq-scrollbar-y a.rtq-scrollbar-down"));

}   

 

public WebElement getScrollbar_TemplateMappingSetting(){

        return SeleniumUtil.waitForElementPresent(driver, By.cssSelector("div#uidialog7.uidialog div.uidialogcontent div#mapEditDlgDiv div.tempmapsetup div.itemsgrid div.rtq-grid div.rtq-grid-sz div.rtq-scrollpanel div.rtq-scrollbar.rtq-scrollbar-y div.rtq-scrollbar-bar"));

 }

/**************************Report Portal–>ReportProductionFlow.java******************************/

 

 

转载于:https://www.cnblogs.com/MasterMonkInTemple/p/4552592.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值