Scroll 黄瓜木偶演员-尝试滚动以使元素可见-滚动不起作用
这是一个黄瓜木偶演员项目的world.js 我意识到我试图用querySelector查找的元素最初不在屏幕上,所以我想向右滚动 所以我添加了滚动代码,但屏幕截图显示滚动并没有发生。 它还显示滚动条是可见的Scroll 黄瓜木偶演员-尝试滚动以使元素可见-滚动不起作用,scroll,cucumber,puppeteer,Scroll,Cucumber,Puppeteer,这是一个黄瓜木偶演员项目的world.js 我意识到我试图用querySelector查找的元素最初不在屏幕上,所以我想向右滚动 所以我添加了滚动代码,但屏幕截图显示滚动并没有发生。 它还显示滚动条是可见的 async getForecastStartColumn(selector, attribute, wait = 0) { let forecast; await this.page.waitFor(wait); await this.page.eval
async getForecastStartColumn(selector, attribute, wait = 0) {
let forecast;
await this.page.waitFor(wait);
await this.page.evaluate(_ => {
window.scrollBy(3000, 0);
});
await this.page.waitFor(1000);
await this.page.screenshot({ path: './world-177.png', fullPage: true });
result = await this.page.document.querySelector('div[class="ag-header-cell ag-header-cell-sortable
grid-column-header-forecast"]');
}
//rest of code omitted
尝试此代码向右滚动
const left = $(document).outerWidth() - $(window).width();
$('body, html').scrollLeft(left);
编辑1
我使用的是纯js而不是jQuery。您应该知道向右滚动的元素,并用该元素的选择器替换可以向右滚动的元素
我使用的是.scrollTo(xpos,ypos)
可以滚动到左侧、右侧、顶部和按钮。使用.scrollWidth
这是元素内容的宽度(以像素为单位)。并使用.pageYOffset
获取元素的当前y
偏移量,以在滚动期间保持此位置。因为.scrollTo(xpo,ypo)
需要x
和y
。我们只需要更改xpos
,并保留ypos
值
你好,史蒂文,你能给我提供一个URL或例子,让我可以尝试在我的设备上本地解决你的问题。不幸的是,这个网站需要登录,我没有颁发凭据的权限。我制作了一个短片来演示这个问题。这里是链接,感谢您的回复。1.我需要jquery吗?你为什么叫它向左滚动。我想向右滚动。谢谢。我更新了我的代码。1.不需要使用jQuery。2.在jQuery中,要向左或向右滚动,应该使用
scrollLeft()
方法。假设您使用scrollLeft(0)
将转到左侧。当您设置位置大数字时,它将向右向左滚动(10000)
。
async getForecastStartColumn(selector, attribute, wait = 0) {
let forecast;
await this.page.waitFor(wait);
await this.page.evaluate(_ => {
const left = $(document).outerWidth() - $(window).width();
$('body, html').scrollLeft(left);
});
await this.page.waitFor(1000);
await this.page.screenshot({ path: './world-177.png', fullPage: true });
result = await this.page.document.querySelector('div[class="ag-header-cell ag-header-cell-sortable
grid-column-header-forecast"]');
}
//rest of code omitted
async getForecastStartColumn(selector, attribute, wait = 0) {
let forecast;
await this.page.waitFor(wait);
await this.page.evaluate(_ => {
const terget = document.querySelector('html');
terget.scrollTo(terget.scrollWidth, terget.pageYOffset);
});
await this.page.waitFor(1000);
await this.page.screenshot({ path: './world-177.png', fullPage: true });
result = await this.page.document.querySelector('div[class="ag-header-cell ag-header-cell-sortable
grid-column-header-forecast"]');
}
//rest of code omitted