如何在puppeteer中模糊输入元素?

Ryo*_*hii 7 javascript puppeteer

我正在与人偶一起测试表格。表单具有在输入元素上发生模糊事件时触发的验证。但是,没有API可以模糊人偶中的元素。

我正在尝试聚焦/单击bodydiv元素,但是无法触发onBlur验证。page.click("body")page.focus("body")

现在,我将虚假的点击图片用于火灾模糊事件。但这不是好方法。

export class LoginPage {
  constructor(private page: Page) {}

  async setup(): Promise<void> {
    await this.page.goto(MY_LOGIN_FORM);
  }

  async typeEmail(email: string): Promise<void> {
    await this.page.type("input[name=email]", email);
    await this.blur();
  }

  async typePassword(password: string): Promise<void> {
    await this.page.type("input[name=password]", password);
    await this.blur();
  }

  async clickLoginButton(): Promise<void> {
    await this.page.click("button");
  }

  private async blur(): Promise<void> {
    // HACK
    await this.page.click("img");
  }
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以模糊输入元素而不会被黑客入侵?

小智 8

我认为使用$eval甚至可能更清洁一些:

await page.$eval('input[name=email]', e => e.blur());
Run Code Online (Sandbox Code Playgroud)