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

如何解决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();
    });
  })();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?