NodeJS-错误:连接ECONNREFUSED 127.0.0.1:portchrome-远程接口

问题描述

我使用 chrome-launcher chrome-remote-interface 编写了脚本,以使用Chrome将网页保存为pdf。

它在Windows机器上没有任何问题,但是当我在CentOS 7上尝试时,出现以下错误,我不知道为什么。两者都使用Chrome v86。

在Windows上,我使用NodeJS v12.18.4 在Linux上,我尝试了v15.1和v12.19

SELinux状态:已禁用

我试图检查其他应用程序是否正在使用该端口,并且没有错误

df.fillna(method='ffill',axis=1)

我的代码

node:internal/process/promises:218
    triggeruncaughtException(err,true /* fromPromise */);
    ^

Error: connect ECONNREFUSED 127.0.0.1:43265
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1128:16) {
  errno: -111,code: 'ECONNREFUSED',syscall: 'connect',address: '127.0.0.1',port: 43265
}

如果您还有其他使用Chrome并将其缩放到0.7的方法,请告诉我。

谢谢

解决方法

我尝试单独使用launchChrome()函数,发现这是问题所在。经过研究,我找到了解决方案。我必须在chromeLauncher.launch标志中添加“ --no-sandbox”。

下面是完整的代码:

const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
const file = require('fs');
var check = 1;

(async function() {
    async function launchChrome() {
      return await chromeLauncher.launch({
        chromeFlags: [
          '--no-first-run','--headless','--disable-gpu','--no-sandbox'
        ]
      });
    }

    const chrome = await launchChrome();
    const protocol = await CDP({
      port: chrome.port
    });

    const { DOM,Page,Emulation,Runtime } = protocol;

    await Promise.all([
      Page.enable(),Runtime.enable(),DOM.enable()
    ]);

    const { frameId } = await Page.navigate({ url: 'https://url/' });
    await Page.loadEventFired();
    const script1 = "window.status";
    while(check){
        var result = await Runtime.evaluate({
            expression: script1
        });
        if(result.result.value=='ready_to_print'){
            check = 0;
        }
    }

    let { data } = await Page.printToPDF({
      landscape: false,printBackground: true,scale: 0.7
    });

    file.writeFile('print.pdf',Buffer.from(data,'base64'),'base64',function(err) {
      if (err) {
        console.log(err);
      }

      protocol.close();
      chrome.kill();
    });
  })();

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...