请试试这个。这个对我有用。
public void scrollElementToCenter(WebElement element) {
try {
if (EnvironmentConstants.RUNNING_ON_CHROME) {
((JavascriptExecutor) driver)
.executeScript("arguments[0].scrollIntoView({behavior: \"auto\", block: \"center\", inline: \"nearest\"});", element);
} else {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", element);
}
} catch (UnsupportedOperationException exception) {
//LOGGER.error("UnsupportedOperationException occurred : " + exception.getMessage(), exception);
}
}
对于Chrome浏览器,您可以使用它
((JavascriptExecutor) driver)
.executeScript("arguments[0].scrollIntoView({behavior: \"auto\", block: \"center\", inline: \"nearest\"});", element);