使用 java 以不同方式進行不同的滾動

下面給出的解決方案也可以在其他支援的程式語言中使用,並進行一些語法更

  1. 向下滾動頁面/條/司的網頁,而有自定義滾動條(未瀏覽器滾動)。單擊此處對於演示和檢查滾動條有其獨立元素。

在下面給出的程式碼中傳遞滾動條元素並需要滾動點。

    public static boolean scroll_Page(WebElement webelement, int scrollPoints)
    {
    try
    {
        System.out.println("---------------- Started - scroll_Page ----------------");
        driver = ExecutionSetup.getDriver();
        dragger = new Actions(driver);

        // drag downwards
        int numberOfPixelsToDragTheScrollbarDown = 10;
        for (int i = 10; i < scrollPoints; i = i + numberOfPixelsToDragTheScrollbarDown)
        {
            dragger.moveToElement(webelement).clickAndHold().moveByOffset(0, numberOfPixelsToDragTheScrollbarDown).release(webelement).build().perform();
        }
        Thread.sleep(500);
        System.out.println("---------------- Ending - scroll_Page ----------------");
        return true;
    }
    catch (Exception e)
    {
        System.out.println("---------------- scroll is unsucessfully done in scroll_Page ----------------");
        e.printStackTrace();
        return false;
    }
  }
  1. 向上滾動頁面/節/部門在網頁同時有自定義滾動條(未瀏覽器滾動)。單擊此處對於演示和檢查滾動條有其獨立元素。

在下面給出的程式碼中傳遞滾動條元素並需要滾動點。

public static boolean scroll_Page_Up(WebElement webelement, int scrollPoints)
{
    try
    {
        System.out.println("---------------- Started - scroll_Page_Up ----------------");
        driver = ExecutionSetup.getDriver();
        dragger = new Actions(driver);
        // drag upwards
        int numberOfPixelsToDragTheScrollbarUp = -10;
        for (int i = scrollPoints; i > 10; i = i + numberOfPixelsToDragTheScrollbarUp)
        {
            dragger.moveToElement(webelement).clickAndHold().moveByOffset(0, numberOfPixelsToDragTheScrollbarUp).release(webelement).build().perform();
        }
        System.out.println("---------------- Ending - scroll_Page_Up ----------------");
        return true;
    }
    catch (Exception e)
    {
        System.out.println("---------------- scroll is unsucessfully done in scroll_Page_Up----------------");
        e.printStackTrace();
        return false;
    }
}
  1. 若要在多個瀏覽器滾動 (內建瀏覽器)時向下滾動,並且你想要使用 Page Down Key 向下滾動。點選這裡進行演示

在下面給出的程式碼中,傳遞滾動區域元素,如 <div>,並需要向下翻頁鍵。

 public static boolean pageDown_New(WebElement webeScrollArea, int iLoopCount)
 {
    try
    {
        System.out.println("---------------- Started - pageDown_New ----------------");
        driver = ExecutionSetup.getDriver();
        dragger = new Actions(driver);

        for (int i = 0; i <= iLoopCount; i++)
        {
            dragger.moveToElement(webeScrollArea).click().sendKeys(Keys.PAGE_DOWN).build().perform();
        }
        System.out.println"---------------- Ending - pageDown_New ----------------");
        return true;
    }
    catch (Exception e)
    {
        System.out.println("---------------- Not able to do page down ----------------");
        return false;
    }
}
  1. 多個瀏覽器滾動 (內建瀏覽器)並且你想要使用 Page UP 鍵向上滾動,請向上滾動。點選這裡進行演示

在下面給出的程式碼中傳遞你的滾動區域元素,如 <div>,並需要向上翻頁鍵。

public static boolean pageUp_New(WebElement webeScrollArea, int iLoopCount)
{
    try
    {
        System.out.println("---------------- Started - pageUp_New ----------------");
        driver = ExecutionSetup.getDriver();
        dragger = new Actions(driver);

        for (int i = 0; i <= iLoopCount; i++)
        {
            dragger.moveToElement(webeScrollArea).click().sendKeys(Keys.PAGE_UP).build().perform();
        }
        System.out.println("---------------- Ending - pageUp_New ----------------");
        return true;
    }
    catch (Exception e)
    {
        System.out.println("---------------- Not able to do page up ----------------");
        return false;
    }
}
  1. 若要在多個瀏覽器滾動 (內建瀏覽器)時向下滾動,並且你想要使用僅向下箭頭鍵向下滾動。點選這裡進行演示

在下面給出的程式碼中傳遞你的滾動區域元素,如 <div> 和 require down 鍵。

public static boolean scrollDown_Keys(WebElement webeScrollArea, int iLoopCount)
{
    try
    {
        System.out.println("---------------- Started - scrollDown_Keys ----------------");
        driver = ExecutionSetup.getDriver();
        dragger = new Actions(driver);

        for (int i = 0; i <= iLoopCount; i++)
        {
            dragger.moveToElement(webeScrollArea).click().sendKeys(Keys.DOWN).build().perform();
        }
        System.out.println("---------------- Ending - scrollDown_Keys ----------------");
        return true;
    }
    catch (Exception e)
    {
        System.out.println("---------------- Not able to do scroll down with keys----------------");
        return false;
    }
}
  1. 若要在多個瀏覽器滾動 (內建瀏覽器)時向上滾動,並且你想要使用僅向上箭頭鍵向上滾動。點選這裡進行演示

在下面給出的程式碼中傳遞你的滾動區域元素,如 <div> 和 require up 鍵。

public static boolean scrollUp_Keys(WebElement webeScrollArea, int iLoopCount)
{
    try
    {
        System.out.println("---------------- Started - scrollUp_Keys ----------------");
        driver = ExecutionSetup.getDriver();
        dragger = new Actions(driver);

        for (int i = 0; i <= iLoopCount; i++)
        {
            dragger.moveToElement(webeScrollArea).click().sendKeys(Keys.UP).build().perform();
        }
        System.out.println("---------------- Ending - scrollUp_Keys ----------------");
        return true;
    }
    catch (Exception e)
    {
        System.out.println("---------------- Not able to do scroll up with keys----------------");
        return false;
    }
}
  1. 要在瀏覽器滾動 (內建瀏覽器) 時向上/向下滾動,並且要僅使用固定點向上/向下滾動。點選這裡進行演示

在下面給出的程式碼中傳遞滾動點。正面意味著向下,負面意味著向上滾動。

public static boolean scroll_without_WebE(int scrollPoint)
{
    JavascriptExecutor jse;
    try
    {
        System.out.println("---------------- Started - scroll_without_WebE ----------------");

        driver = ExecutionSetup.getDriver();
        jse = (JavascriptExecutor) driver;
        jse.executeScript("window.scrollBy(0," + scrollPoint + ")", "");

        System.out.println("---------------- Ending - scroll_without_WebE ----------------");
        return true;
    }
    catch (Exception e)
    {
        System.out.println("---------------- scroll is unsucessful in scroll_without_WebE ----------------");
        e.printStackTrace();
        return false;
    }
}
  1. 要在瀏覽器滾動 (內建瀏覽器) 時向上/向下滾動,並且要向上/向下滾動到可見區域中的 make 元素或動態滾動點選這裡進行演示

在下面給出的程式碼中傳遞你的元素。

public static boolean scroll_to_WebE(WebElement webe)
{
    try
    {
        System.out.println("---------------- Started - scroll_to_WebE ----------------");

        driver = ExecutionSetup.getDriver();
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", webe);

        System.out.println("---------------- Ending - scroll_to_WebE ----------------");
        return true;
    }
    catch (Exception e)
    {
        System.out.println("---------------- scroll is unsucessful in scroll_to_WebE ----------------");
        e.printStackTrace();
        return false;
    }
}

注意:請驗證你的案例和使用方法。如果遺漏任何案件,請告訴我