所以我尝试使用已经存在的 chrome 配置文件启动 puppeteer 并且它可以工作,但我想做的是多次启动相同的进程,这会引发错误
(节点:12820)UnhandledPromiseRejectionWarning:错误:无法启动浏览器进程![0311/152606.490:ERROR:chrome_main_delegate.cc(679)] 仅当“--user-data-dir”也指定为非默认值时,才能禁用 Web 安全
我这样做:
const browser = await puppeteer.launch({
executablePath: "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
headless: false,
// excludeSwitches: 'enable-automation',
args: [
'--user-data-dir=C:\\Users\\USER\\AppData\\Local\\Google\\Chrome\\User Data'
],
});
Run Code Online (Sandbox Code Playgroud) 我想要的是获取一个 cookie 并在我想打开一个已经登录的 puppeteer 实例时重新使用它。我试过这个,但没有用:
获取 cookie 的函数:
async function writeCookies(page, cookiesPath) {
const client = await page.target().createCDPSession();
// This gets all cookies from all URLs, not just the current URL
const cookies = (await client.send("Network.getAllCookies"))["cookies"];
console.log(cookies)
console.log("Saving", cookies.length, "cookies");
// fs.writeFileSync(cookiesPath, JSON.stringify(cookies));
console.log(cookiesPath)
console.log(cookies)
// await fs.writeJSON(cookiesPath, cookies);
}
Run Code Online (Sandbox Code Playgroud)
我是如何实施的:
const page = await browser.newPage();
time = new Date()
await page.goto("https://accounts.google.com/ServiceLogin/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1<mpl=default<mplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=AddSession")
await page.$eval('#identifierId', (el) => (el.value = "test@gmail.com"));
await page.click("#identifierNext > div > button > div.VfPpkd-RLmnJb");
await page.waitForSelector('#password > …Run Code Online (Sandbox Code Playgroud) 所以我正在与 puppeteer 合作来实现自动化,它工作得很好,但是当我加载网站时,它比我的正常网站需要更多的时间来加载,我尝试使用这个进行缓存
const puppeteer = require('puppeteer');
let time = new Date()
async function test() {
const browser = await puppeteer.launch({
headless: true,
executablePath:"D:\\Desktop\\node_modules\\puppeteer\\.local-chromium\\win64-848005\\chrome-win\\chrome.exe",
args: ['--no-sandbox'],
});
const page = await browser.newPage();
const response = await page.goto('https://example.com/');
console.log(`${new Date() -time }`)
console.log(response);
await browser.close();
}
Run Code Online (Sandbox Code Playgroud)
它适用于 example.com 存储了缓存并且加载速度更快,但我的目标网站似乎不允许缓存存储
还有其他方法可以加快进程吗?
所以我正在做的是,我有 2 个文件,一个包含一个脚本,可以生成一个令牌,第二个文件处理该令牌。
问题在于,记录令牌的第二个脚本只会记录收到的第一个令牌。
这是我处理令牌的方式:
const first_file = require("./first_file.js");
first_file.first_file().then((res) => {
console.log(res);
});
Run Code Online (Sandbox Code Playgroud)
显然这是行不通的,因为它没有用更新的值更新。
first_file = async () => {
return new Promise(async (resolve, reject) => {
//Generating the token
(async () => {
while (true) {
console.log("Resolving...");
resolve(token);
await sleep(5000);
resolved_token = token;
}
})();
});
};
module.exports = { first_file };
Run Code Online (Sandbox Code Playgroud)
我在这里做的是,我尝试做一个,while..loop以便我继续解析令牌。但它没有,有没有办法直接导出变量,这样任务会更容易?
所以我想做的是用我的谷歌个人资料打开 puppeteer 窗口,但我想要的是多次执行它,我的意思是 2-4 个窗口但具有相同的个人资料 - 这可能吗?当我这样做时,我收到此错误:
(node:17460) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
[45844:13176:0410/181437.893:ERROR:cache_util_win.cc(20)] Unable to move the cache: Access is denied. (0x5)
Run Code Online (Sandbox Code Playgroud)
(node:17460) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
[45844:13176:0410/181437.893:ERROR:cache_util_win.cc(20)] Unable to move the cache: Access is denied. (0x5)
Run Code Online (Sandbox Code Playgroud) javascript ×5
node.js ×4
puppeteer ×4
automation ×3
caching ×1
cookies ×1
generator ×1
promise ×1