Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome 处理警报_Google Chrome_Go_Web Scraping - Fatal编程技术网

Google chrome 处理警报

Google chrome 处理警报,google-chrome,go,web-scraping,Google Chrome,Go,Web Scraping,如何使用chromedp捕捉网页上显示的警告框并获取其中的文本 我注意到,当显示警报时,我可以看到Page.javascriptDialogOpening正在显示 我正在使用 cdp.evaluatesdevtools(“Page.javascriptDialogOpening”,res) 将文本放入其中,但它不起作用 如何在chromedp中处理它???我做了一个变通方法,先将一些javascript硬编码到浏览器中,然后在控制台中收听警报框文本 以下代码仅供参考: func main()

如何使用chromedp捕捉网页上显示的警告框并获取其中的文本

我注意到,当显示警报时,我可以看到Page.javascriptDialogOpening正在显示

我正在使用

cdp.evaluatesdevtools(“Page.javascriptDialogOpening”,res)
将文本放入其中,但它不起作用
如何在chromedp中处理它???

我做了一个变通方法,先将一些javascript硬编码到浏览器中,然后在控制台中收听警报框文本

以下代码仅供参考:

func main() {
    // create context
    ctx, cancel := chromedp.NewContext(context.Background())
    defer cancel()

    // run task list
    var res interface{}
    err := chromedp.Run(ctx,
    chromedp.Navigate(`https://www.quackit.com/javascript/javascript_alert_box.cfm`), // navigate to random page
    chromedp.EvaluateAsDevTools(`window.alert = function (txt){return txt}`, &res), // set a function to return the text in the alert box as text
    chromedp.EvaluateAsDevTools(`alert('hehe')`, &res), // create an alert box to test the execution
)
    if err != nil {
        log.Fatal(err)
    }
    log.Println(res)
}

它将登录到您的控制台res。
希望有帮助;)

在任务内部,使用ListenTarget并等待JS对话框事件

printMsg := chromedp.ActionFunc(func(ctx context.Context) error {


    chromedp.ListenTarget(lctx, func(ev interface{}) {

        if _, ok := ev.(*page.EventJavascriptDialogOpening); ok { // page loaded

            fmt.Printf(ev.(*page.EventJavascriptDialogOpening).Message) // holds msg!
        }
    })

}

这有点旧,但可能会有所帮助:我想要特定的代码示例