Selenium moveToElement() 到硒化物

Selenium moveToElement() to Selenide

我从 Selenide 开始,想知道是否可以滚动到该元素。在 Seleniu 中,代码如下所示:

    Actions actions = new Actions(getWebDriver());
    actions.moveToElement(loadMore).build().perform();

我需要滚动到元素,因为如果它在屏幕之外则无法点击。

这还不够吗?

actions.moveToElement(loadMore).click().build().perform()

你应该能够点击一个元素,即使它在屏幕之外(确保 XPath 是正确的!) 您可以使用 actions.scrollIntoView(actions.findElement(By.xpath(xpath))

Selenide 有 scrollIntoView 方法,实现 JavaScript 的 scrollIntoView 并将元素滚动到特定位置:

// the top of the element will be aligned to the top.
$("").scrollIntoView(true).click();

// element will be aligned to the center. 
$("").scrollIntoView("{behavior: \"instant\", block: \"center\", inline: \"center\"}").click();