简体   繁体   English

Node.js: puppeteer focus() 函数

[英]Node.js: puppeteer focus() function

I am trying to login on the site using puppeteer and then some other stuff after I am logged in. Connection to site was successful, but I have problem with function focus().我正在尝试使用 puppeteer 登录该站点,然后在登录后使用其他一些东西。与站点的连接成功,但我的函数 focus() 有问题。 It needs a selector as an parameter, but after inserting one, it show an error (selector is good, because I ran document.querySelector("input.login-field") in console of the site and returned this: <input class="login-field" type="text" inputmode="email" autocapitalize="none" name="m" placeholder="Email or username" value=""> ).它需要一个选择器作为参数,但是在插入一个选择器后,它显示一个错误(选择器很好,因为我在站点的控制台中运行了 document.querySelector("input.login-field") 并返回了这个: <input class="login-field" type="text" inputmode="email" autocapitalize="none" name="m" placeholder="Email or username" value=""> )。 What's the problem?有什么问题?

Here's my code:这是我的代码:

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();

})();

If you're sure that the selector is good and it's working in the console in headful mode, try to wait until the page scripts are downloaded, started, and the needed element appeared in the DOM:如果您确定选择器是好的并且它在控制台中以 headful 模式工作,请尝试等待页面脚本下载、启动并且所需元素出现在 DOM 中:

await page.goto("site");
await page.waitForSelector('input.login-field'); // <-- wait until it exists
await page.focus("input.login-field");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM