vla*_*_sl 2 javascript node.js web puppeteer
我正在尝试使用 puppeteer 登录该网站,然后在登录后使用其他一些东西。连接到网站成功,但我在功能 focus() 方面遇到问题。它需要一个选择器作为参数,但插入一个后,它显示一个错误(选择器很好,因为我在站点的控制台中运行了 document.querySelector("input.login-field") 并返回了这个:)
<input class="login-field" type="text" inputmode="email" autocapitalize="none" name="m" placeholder="Email or username" value="">
。有什么问题?
这是我的代码:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless: false, slowMo: 25});
const page = await browser.newPage();
await page.goto("site");
await page.focus("input.login-field");
await page.keyboard.type("information");
await browser.close();
})();
Run Code Online (Sandbox Code Playgroud)
如果您确定选择器是好的并且它在控制台中以 headful 模式工作,请尝试等到页面脚本下载、启动并且所需的元素出现在 DOM 中:
await page.goto("site");
await page.waitForSelector('input.login-field'); // <-- wait until it exists
await page.focus("input.login-field");
Run Code Online (Sandbox Code Playgroud)