zen
zen

Reputation: 1165

Confirming or Dismissing Dialog when using Puppeteer

I'm trying to dismiss a dialog within Puppeteer. I tried to convert original Puppeteer instructions, but did not work.

The website I'm crawling throws up the alert from within body onload.

Here is what I have so far with no luck.

$js_function = JsFunction::createWithAsync()
     ->body('async dialog => {
     await dialog.dismiss();
}');

$this->page->on('dialog', $js_function);

$this->page->goto($this->url);

Original puppeteer docs: https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#class-dialog

Upvotes: 1

Views: 876

Answers (1)

Oleh Demkiv
Oleh Demkiv

Reputation: 1431

You did almost everything right, but there is one small bug.

Here correct code:

$js_function = JsFunction::createWithAsync()
    ->parameters(['dialog'])
    ->body('await dialog.dismiss();');

$this->page->on('dialog', $js_function);

$this->page->goto($this->url);

And here example of code, if you wanna confirm:

$js_function = JsFunction::createWithAsync()
    ->parameters(['dialog'])
    ->body('await dialog.accept();');

$this->page->on('dialog', $js_function);

$this->page->goto($this->url);

Upvotes: 2

Related Questions