2 回答
TA贡献1871条经验 获得超13个赞
尝试以下选项来水平滚动:
// scroll at a particular element
WebElement e1 =
driver.findElement(By.xpath(“//input [@id=’ElementID’]”));
((JavascriptExecutor)driver).executeScript(“arguments[0].scrollIntoView();”, e1);
Where ‘e1’ is the locator where you want to scroll.
// scroll at a particular coordinate,.
((JavascriptExecutor)driver).executeScript(“window.scrollBy(200,300)”);
// scroll horizontally in the right direction.
((JavascriptExecutor)driver).executeScript(“window.scrollBy(2000,0)”);
// scroll horizontally in the left direction.
((JavascriptExecutor)driver).executeScript(“window.scrollBy(-2000,0)”);
TA贡献1877条经验 获得超1个赞
这个想法很简单 - 首先,尝试将滚动条移动到任一侧并检查它是否真的移动了。
//scrol 1px to the left
$(document).scrollLeft(1);
if($(document).scrollLeft() != 0){
//there's a scroll bar
}else{
//there's no scrollbar
}
//scroll back to original location
$(document).scrollLeft(0);
让我们将其转换为 Java 方法并在您的脚本中使用它。
/**
*
* @param js reference to JavascriptExecutor
* @param element is a string of locator (i.e. #scroll > span etc.)
* @return true if scrollbar is scrollable
*/
boolean testScrollBar(JavascriptExecutor js, String element){
String jq = String.format("return $('%s').scrollLeft()", element);
String jq1 = String.format("$('%s').scrollLeft(1)", element);
String jq2 = String.format("$('%s').scrollLeft(0)", element);
System.err.println("Test1: "+js.executeScript(jq));
js.executeScript(jq1);
Long currentPos = (Long)js.executeScript(jq);
System.err.println("Test2: " + currentPos);
js.executeScript(jq2);
return currentPos != 0;
}
我希望它能帮助你。
添加回答
举报