1
I’m trying to run a pyppeteer code (basically Puppeteer but for python instead of nodejs), and in it I run a script in the browser initialized through . evaluate(), but something in my javascript is causing the error cannot read Property 'getBoundingClientRect' of Undefined.
I’m running four different evals because joining them doesn’t even work on the Chrome console directly. They are:
await page.evaluate('''function offset(el) {
var rect = el.getBoundingClientRect(),
scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
scrollTop = window.pageYOffset || document.documentElement.scrollTop;
return { top: rect.top + scrollTop, left: rect.left + scrollLeft }
}''')
await page.evaluate('''var vari1 = document.querySelector('.swap-wrap');''')
await page.evaluate('''var divOffset = offset(vari1);''')
element_coordinates = await page.evaluate('''() => {
return(divOffset.left, divOffset.top)
}''')
Does the fact that the code is asynchronous have to do with (supposedly) the el be treated as Undefined? If I run on the console each of these commands contained in Eval one at a time, I will have the desired result, but here with Eval not.
Code in full: https://ideone.com/zddnWR
And the whole mistake: https://imgur.com/a/3130PW7
I added this line to my code, and now it raises offset is not defined error on the line
await page.evaluate('''var divOffset = offset(vari1);''')
– user93774