適用場景
當(dāng)執(zhí)行遇到錯誤時需要進(jìn)行錯誤解圖同波,通過滾動到有效元素進(jìn)行截圖,截圖更有意義。
通過JS語句滾動
1. 滾動到頁面底部
public void scrollToPageButtom(){
try{
( (JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
Thread.sleep(3000);
}catch(InterruptedException e)
e.printStackTrace();
}
2. 滾動到指定元素
public void scrollToElement(WebElement element){
try{
( (JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
Thread.sleep(3000);
}catch(InterruptedException e)
e.printStackTrace();
}
3. 滾動到指定坐標(biāo)(x,y)
public void scrollToCoordinates(int x, int y){
try{
( (JavascriptExecutor) driver).executeScript("window.scrollBy("+x+","+y+")");
Thread.sleep(3000);
}catch(InterruptedException e)
e.printStackTrace();
}
PS: Thread.sleep(3000); 是為了讓每次執(zhí)行時線程強(qiáng)制等待3秒垦垂,以免還沒滾動到指定位置沪哺,就已經(jīng)進(jìn)行下一步