2 回答
TA贡献1934条经验 获得超2个赞
您可以使用我编写的以下方法滚动到特定元素。您只需要传递 Driver 对象。
// Method to scroll down to specific element
public static void scrollToElement(WebDriver driver) throws InterruptedException {
String key = "";
WebElement element = null;
JavascriptExecutor js = (JavascriptExecutor) driver;
element = driver.findElement(By.xpath(locator));
// This will scroll the page till the element is found
js.executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(2000);
}
TA贡献1890条经验 获得超9个赞
你能试试这个吗,我希望window你的代码中缺少全局变量
// scroll vertically
js.executeScript("window.scrollTo(0, 1000);")
// scroll horizontally
js.executeScript("window.scrollTo(1000, 0);")
//scroll to particular element
WebElement Element = driver.findElement(By.id("someID"));
js.executeScript("arguments[0].scrollIntoView();", Element);
添加回答
举报