首页 > 后端开发 > Golang > 正文

chromedp click 在我的 golang 代码中不起作用。你能找出问题所在吗?

PHPz
发布: 2024-02-10 09:54:10
转载
547 人浏览过

chromedp click 在我的 golang 代码中不起作用。你能找出问题所在吗?

php小编草莓,你好!关于你提到的问题,chromedp click 在你的 golang 代码中无法起作用的情况,我可以帮你找出问题所在。chromedp 是一个使用 Chrome DevTools Protocol 进行自动化的库,click 方法用于模拟鼠标点击事件。可能的问题有:1. 页面元素不可见或被其他元素遮挡,导致 click 失效;2. click 方法的参数传递有误;3. chromedp 的版本与 Chrome 浏览器版本不兼容;4. 其他代码逻辑问题。请提供更多细节,我将尽快给出解决方案。

问题内容

我正在使用 chromedp 开发 scrapper。

要获得我想要的内容(页面 html),我必须单击特定按钮。

所以我使用了 chromedp.click 和 chromedp.outerhtml,但我只得到了点击前页面的 html,而不是点击完成后页面的 html。

你能看到我的代码并建议我如何修复它吗?

func runCrawler(URL string, lineNum string, stationNm string) {
    
        // settings for crawling
    opts := append(chromedp.DefaultExecAllocatorOptions[:],
        chromedp.Flag("headless", false))
    
        // create chrome instance
    contextVar, cancelFunc := chromedp.NewExecAllocator(context.Background(), opts...)
    defer cancelFunc()

    contextVar, cancelFunc = chromedp.NewContext(contextVar)
    defer cancelFunc()


    var htmlContent string

    err := chromedp.Run(contextVar,
        chromedp.Navigate(URL),
        chromedp.WaitVisible(".end_footer_area"),
        chromedp.Click(".end_section.station_info_section > div.at_end.sofzqce > div > div.c10jv2ep.wrap_btn_schedule.schedule_time > button"),
        chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery),
    )
    fmt.Println("html", htmlContent)
    checkErr(err)
登录后复制

我还给你主页和我需要点击的按钮

页面网址:https://pts.map.naver.com/end-subway/ends/web/11321/home

我需要点击的按钮区域:

非常感谢

解决方法

您想要获取的页面已在新选项卡(目标)中打开。

在这种情况下,我们可以使用 chromedp.waitnewtarget 来创建一个 chan,从中我们可以接收新选项卡的目标 id。然后使用 chromedp.withtargetid 选项创建一个新上下文,以便我们可以连接到新选项卡。从这里开始,一切都是您已经熟悉的。

package main

import (
    "context"
    "fmt"
    "strings"

    "github.com/chromedp/cdproto/target"
    "github.com/chromedp/chromedp"
)

func main() {
    opts := append(chromedp.DefaultExecAllocatorOptions[:],
        chromedp.Flag("headless", false),
    )

    ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
    defer cancel()

    ctx, cancel = chromedp.NewContext(ctx)
    defer cancel()

    var htmlContent string

    ch := chromedp.WaitNewTarget(ctx, func(i *target.Info) bool {
        return strings.Contains(i.URL, "/timetable/web/")
    })

    err := chromedp.Run(ctx,
        chromedp.Navigate("https://pts.map.naver.com/end-subway/ends/web/11321/home"),
        chromedp.WaitVisible(".end_footer_area"),
        chromedp.Click(".end_section.station_info_section > div.at_end.sofzqce > div > div.c10jv2ep.wrap_btn_schedule.schedule_time > button"),
    )
    if err != nil {
        panic(err)
    }

    newCtx, cancel := chromedp.NewContext(ctx, chromedp.WithTargetID(<-ch))
    defer cancel()

    if err := chromedp.Run(newCtx,
        chromedp.WaitReady(".table_schedule", chromedp.ByQuery),
        chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery),
    ); err != nil {
        panic(err)
    }
    fmt.Println("html", htmlContent)
}
登录后复制

以上是chromedp click 在我的 golang 代码中不起作用。你能找出问题所在吗?的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!