按 F11 浏览器全屏后高度会增加,尤其需要注意
function f(){ document.querySelector('div[widgetname=FORM]').style='';
document.querySelector('div[widgetname=BODY]').style='';
}
f()
var body = document.body,
html = document.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
height

# 这段代码去掉滚动条
await page.evaluate('''() =>{ document.querySelector('div[widgetname=FORM]').style='';
document.querySelector('div[widgetname=BODY]').style='';
}''')
js = '''
() => {
var body = document.body,
html = document.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
return height
}
'''
# 获取执行完 js 后的页面高度
h = await page.evaluate(js)
h = h + 83
print('h', h)
await page.setViewport({'width': width, 'height': h})
参考
看 Example 1
https://pptr.dev/api/puppeteer.page.evaluate/