简体   繁体   English

getBoundingClientRect()导致javascript崩溃?

[英]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. 在谷歌C​​hrome控制台中它返回此。

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.

相关问题 是什么导致 getBoundingClientRect() 中的故障? - What causes the glitch in getBoundingClientRect()? 如何确定是什么 JavaScript 导致浏览器崩溃? - How to determine what JavaScript causes a browser crash? 在Eclipse中重构Javascript“extract方法”会导致崩溃 - Refactoring Javascript “extract method” in Eclipse causes a crash 带有RegEx的Javascript .replace()导致浏览器挂起/崩溃 - Javascript .replace() with RegEx causes browser to hang/crash 滚动时JavaScript getBoundingClientRect()会发生变化 - JavaScript getBoundingClientRect() changes while scrolling 在循环一次之前,Javascript 会在 for 循环开始时导致崩溃 - Javascript causes crash at start of for-loop before ever looping once Javascript getBoundingClientRect()-适用于类的多个实例 - Javascript getBoundingClientRect() - apply to multiple instances of class 使用 Javascript getBoundingClientRect 将项目对齐到网格 - Using Javascript getBoundingClientRect to Snap Items to Grid 顶部和左侧坐标在 getBoundingClientRect() 上不起作用 - Javascript - Top and Left Coordinates Not Working On getBoundingClientRect() - Javascript JavaScript canvas-clearImage()-canvas.getBoundingClientRect - JavaScript canvas - clearImage() - canvas.getBoundingClientRect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM