Nightwatch中有一个本地方法可以将元素滚动到视图中。总的来说,应该始终从nightwatch/selenium中滚动元素以获得更好的效果。但是如果您想自己手动实现,可以使用getLocationInView() :
return this.getLocationInView('#myElement')
.assert.visible('#myElement')
.click('#myElement')
Nightwatch也支持直接通过Webdriver协议使用moveTo()来实现此操作,而不需要任何抽象。在这种情况下,它看起来更像是:
const self = this;
return this.api.element('#myElement', (res) => {
self.api.moveTo(res.value.ELEMENT, 0, 0, () => {
self.assert.visible('#myElement');
self.click('#myElement');
})
});
但在您的情况下有助于改变配置中Selenium元素滚动行为,例如:
firefox: {
desiredCapabilities: {
browserName: 'firefox',
javascriptEnabled: true,
acceptSslCerts: true,
elementScrollBehavior: 1
}
}
默认值为0 -> 元素滚动到页面顶部
elementScrollBavior为1 -> 元素滚动到页面底部