[英]getBoundingClientRect() causes javascript to crash?
So I am trying to scrape a captcha image off a page using the following code within my phantom.js 所以我试图在我的phantom.js中使用以下代码从页面上刮掉验证码图像
if(page.content.search('captcha') != -1){
console.log('taking a picture')
var clipRect = document.querySelector("img[id='auth-captcha-image']").getBoundingClientRect();
console.log('got bounds')
page.clipRect = {
top: clipRect.top,
left: clipRect.left,
width: clipRect.width,
height: clipRect.height
};
page.render('capture.png');
}
As you can see its pretty straight forward. 你可以看到它很直接。 Get the element, get clip to the bounds of the element and then take a screenshot of the render. 获取元素,获取元素边界的剪辑,然后获取渲染的屏幕截图。
Now when I execute 现在当我执行
document.querySelector("img[id='auth-captcha-image']").getBoundingClientRect()
within the google chrome console it returns this. 在谷歌Chrome控制台中它返回此。
ClientRect {top: 430, right: 621, bottom: 500, left: 421, width: 200…}
However within my javascript code it seems to be producing some sort of error that I cannot catch. 但是在我的javascript代码中,它似乎产生了一些我无法捕获的错误。 When I remove the suffix ".getBoundingClientRect()" the code starts working. 当我删除后缀“.getBoundingClientRect()”时,代码开始工作。 (obviously I have to add dummy values in for the clipRect dimensions). (显然我必须为clipRect维度添加虚拟值)。
I dont quite understand how it is crashing and how to even see the stacktrace/error message of the crash.. 我不太明白它是如何崩溃以及如何甚至看到崩溃的堆栈跟踪/错误消息..
Any help would be amazing Thanks 任何帮助都会很棒谢谢
Any and all javascript that has to do with the target page's DOM must be executed inside page.evaluate() function. 任何和所有与目标页面的DOM有关的javascript必须在page.evaluate()函数内执行。
var clipRect = page.evaluate(function(){
return document.querySelector("img[id='auth-captcha-image']").getBoundingClientRect();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.