使用 java 以不同方式进行不同的滚动
下面给出的解决方案也可以在其他支持的编程语言中使用,并进行一些语法更
- 做向下滚动页面/条/司的网页,而有自定义滚动条(未浏览器滚动)。单击此处对于演示和检查滚动条有其独立元素。
在下面给出的代码中传递滚动条元素并需要滚动点。
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;
}
}
- 做向上滚动页面/节/部门在网页同时有自定义滚动条(未浏览器滚动)。单击此处对于演示和检查滚动条有其独立元素。
在下面给出的代码中传递滚动条元素并需要滚动点。
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;
}
}
- 若要在多个浏览器滚动 (内置浏览器)时向下滚动,并且你想要使用 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;
}
}
- 若多个浏览器滚动 (内置浏览器)并且你想要使用 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;
}
}
- 若要在多个浏览器滚动 (内置浏览器)时向下滚动,并且你想要使用仅向下箭头键向下滚动。点击这里进行演示
在下面给出的代码中传递你的滚动区域元素,如
<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;
}
}
- 若要在多个浏览器滚动 (内置浏览器)时向上滚动,并且你想要使用仅向上箭头键向上滚动。点击这里进行演示
在下面给出的代码中传递你的滚动区域元素,如
<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;
}
}
- 要在浏览器滚动 (内置浏览器) 时向上/向下滚动,并且要仅使用固定点向上/向下滚动。点击这里进行演示
在下面给出的代码中传递滚动点。正面意味着向下,负面意味着向上滚动。
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;
}
}
- 要在浏览器滚动 (内置浏览器) 时向上/向下滚动,并且要向上/向下滚动到可见区域中的 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;
}
}