- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试为 Puppeteer 项目提供 GUI。我考虑过使用 Electron,但遇到错误:
Error: Passed function is not well-serializable!
当运行 Puppeteer 函数时:
await page.waitForSelector('.modal', { visible: true });
我在处理 page.evaluate()
时找到了一种正确的序列化方法,但是在 page.waitForSelector()
的情况下如何继续?
Puppeter 的 API 函数是否可以在需要时正确序列化?
编辑
我决定重写
await page.waitForSelector('.modal', { visible: true });
使用 page.evaluate,代码如下:
// first recreate waitForSelector
const awaitSelector = async (selector) => {
return await new Promise(resolve => {
const selectorInterval = setInterval(() => {
if ($(selector).is(':visible')) {
console.log(`${selector} visible`);
resolve();
clearInterval(selectorInterval);
};
}, 1000);
});
}
然后使用 page.evaluate() 调用该函数:
// remember to pass over selector's name, in this case it is ".modal"
await page.evaluate('(' + awaitSelector.toString() + ')(".modal");');
最佳答案
首先是上下文:通常你不能从浏览器环境运行 puppeteer,它只能在 nodejs 中运行。 Electron 提供了 2 个进程 renderer 和 main。每当你想使用节点时,你都必须在主节点中使用它。
关于两个进程之间的通信,您可以在文档中阅读,有很多处理方法。据我所知,最佳做法是在预加载中声明它并使用 ipc 桥。其他解决方案违反了 contextIsolation 规则。
我在一个问题和另一个问题之间徘徊:比如不可序列化的函数、未定义的需求等等。
最后,我从头开始重写了所有内容,这就是我的解决方案:
主要.js
const { app, BrowserWindow } = require('electron')
const path = require('path')
const { ipcMain } = require('electron');
const puppeteer = require("puppeteer");
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: true,
},
})
ipcMain.handle('ping', async () => {
await checkPup()
})
async function checkPup() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
page
.waitForSelector('h1', { visible: true })
.then(() => {
console.log('got it')
});
const [button] = await page.$x("//button[contains(., 'Create account')]");
if (button) {
console.log('button: ', button)
await button.click();
await page.screenshot({ path: 'tinder.png' });
const [button] = await page.$x("//button[contains(., 'Create account')]");
if (button) {
console.log('button: ', button)
await button.click();
await page.screenshot({ path: 'tinder.png' });
}
}
await browser.close();
}
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// Attach listener in the main process with the given ID
ipcMain.on('request-mainprocess-action', (event, arg) => {
// Displays the object sent from the renderer process:
//{
// message: "Hi",
// someData: "Let's go"
//}
console.log(
arg
);
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
预加载.js
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
const { contextBridge, ipcRenderer } = require('electron')
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
})
contextBridge.exposeInMainWorld('versions', {
node: () => process.versions.node,
chrome: () => process.versions.chrome,
electron: () => process.versions.electron,
ping: () => ipcRenderer.invoke('ping'),
// we can also expose variables, not just functions
})
渲染器.js
const information = document.getElementById('info')
const btn = document.getElementById('btn')
information.innerText = `This app is using Chrome (v${versions.chrome()}), Node.js (v${versions.node()}), and Electron (v${versions.electron()})`
btn.addEventListener('click', () => {
console.log('habad!!!!!')
func()
})
const func = async () => {
const response = await window.versions.ping()
information.innerText = response;
console.log(response) // prints out 'pong'
}
对不起,有点乱,我希望它能帮助别人找到解决其他问题的方法
关于electron - Electron puppeteer 师 : Error: Passed function is not well-serializable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60895833/
我最近刚跨过木偶继承。围绕它的几个问题: 使用p继承是一种好习惯吗?一些经验丰富的木偶同事告诉我,木偶的继承不是很好,我不太相信。 来自OO世界,我真的很想了解木偶继承的原理,以及覆盖的原理。 最佳答
需要一个 puppet 特工联系一些不同 puppet 师。 原因:不同的组创建了不同且独立的 list 集。 可能的组及其任务 应用程序供应商:应用程序的配置 安全性:强化 运营:路由表、监控工具
您可以在 puppet 中制作子模块吗,例如... puppet_root - modules - module_1 - submodule - manifes
在执行类 mypackage 中的代码之前,我需要执行类 mysql,tomcat。在我的 site.pp 我有 node 'node1' { include mysql,mypackage,tomc
我试图从失败的请求和js错误中收集数据。 我正在使用以下网站:https://nitzani1.wixsite.com/marketing-automation/3rd-page 该网站有一个对htt
我在 Suse 11 Linux 机器上使用 puppet 2.7.19 和 facter 1.6.0。 $::osfamily因子变量未设置,尽管它确实有 $::operatingsystem事实集
我正在使用 Puppet 开源版本。 我发现 Puppet 3.7.5 中有几个组件: puppet 大师 puppet 代理 MCollective 希拉 puppet 数据库 但我不知道他们是如何
主要目标是自动添加所有 puppet 模块,以便可以使用一个命令启动所有 dev-env 和 prod-env。如何通过 puppet manifest 安装 puppet 模块? 最佳答案 我们一直
我的 Puppet list 中有一系列 exec: 第一个下载带有二进制文件的 ZIP 文件(除非已经安装了二进制文件)并将其保存到 /tmp。 第二个将其解压缩。 当我第一次应用 list 时,它
我使用 aws opsworks 创建了 puppet master。我可以将 ami linux 节点自动添加到 puppet master。 当我尝试通过此链接 https://puppet.co
关于 this 的另一个问题问题。 有没有办法puppet module install从我已经下载到磁盘的本地存储库安装的工具? 最佳答案 您可以使用Puppet Library托管您自己的私有(p
我可以通过 Puppet Enterprise Master 到 Agent 运行几乎所有其他命令,但更新命令不会运行。据我所知,PE确实以root身份运行。 有人可以建议如何让这个命令在 Linux
我只想在 puppeteer 中刚刚打开的页面中应用标题,而不是在所有内部页面标题中应用标题 page.setExtraHTTPHeaders({ 'X-Just-Must-Be-Req
有没有什么方法可以用 Puppet 的语言声明数组中的包应该按照它们在数组中给出的顺序安装? 我想自动安装 CUDA,这需要 nvidia-driver-latest-dkms、cuda 和 cuda
鉴于这个简化的 Puppet 示例,很明显,由于 ~> 链接运算符,对 my_file 的任何更改都会触发 my_service 刷新: package { 'my_package': } -> fi
我是第一次使用 puppeteer,而不是工程师/程序员! 我已经能够生成我尝试使用 puppeteer 的大部分表单,但是我被单选按钮卡住了。网络表单中的开发人员工具显示该字段的以下内容: 使用:
如果您为代理在后台运行时执行 puppet agent -t,我们可以在其中看到更改的日志文件在哪里,而不是在 puppet 仪表板上。 我查看了 puppet.conf 并在主要部分看到了 logd
我正在尝试使用 exec 资源类型来执行批处理文件。但我想将变量 $dsn_64bit 的值从 init.pp 传递到 install.pp。请让我知道如何实现: 这是我的 init.pp class
我正在尝试使用 list 将用户分配到多个组,但遇到了障碍。 尝试 1: class usergroup { group { "user_one": ensure => present
我想先说我是 puppet 的新手。我一直在通过 vagrant 使用它并且我开始对写作表现感到自在,但我可能缺乏可以回答我的问题的经验或直觉。 我试图掌握 puppet 的范围以及画线的位置。我对这
我是一名优秀的程序员,十分优秀!