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中文网其他相关文章!

Go的strings包提供了多种字符串操作功能。1)使用strings.Contains检查子字符串。2)用strings.Split将字符串分割成子字符串切片。3)通过strings.Join合并字符串。4)用strings.TrimSpace或strings.Trim去除字符串首尾的空白或指定字符。5)用strings.ReplaceAll替换所有指定子字符串。6)使用strings.HasPrefix或strings.HasSuffix检查字符串的前缀或后缀。

使用Go语言的strings包可以提升代码质量。1)使用strings.Join()优雅地连接字符串数组,避免性能开销。2)结合strings.Split()和strings.Contains()处理文本,注意大小写敏感问题。3)避免滥用strings.Replace(),考虑使用正则表达式进行大量替换。4)使用strings.Builder提高频繁拼接字符串的性能。

Go的bytes包提供了多种实用的函数来处理字节切片。1.bytes.Contains用于检查字节切片是否包含特定序列。2.bytes.Split用于将字节切片分割成smallerpieces。3.bytes.Join用于将多个字节切片连接成一个。4.bytes.TrimSpace用于去除字节切片的前后空白。5.bytes.Equal用于比较两个字节切片是否相等。6.bytes.Index用于查找子切片在largerslice中的起始索引。

theEncoding/binarypackageingoisesenebecapeitProvidesAstandArdArdArdArdArdArdArdArdAndWriteBinaryData,确保Cross-cross-platformCompatibilitiational and handhandlingdifferentendenness.itoffersfunctionslikeread,写下,写,dearte,readuvarint,andwriteuvarint,andWriteuvarIntforPreciseControloverBinary

回顾bytespackageingoiscialforhandlingbyteslicesandbuffers,offeringToolsforefficeMemoryManagement和datamAnipulation.1)ItProvidesfunctionalitiesLikeCreatingBuffers,比较,搜索/更换/更换/更换forlargedAtatAsetsets.n

你应该关心Go语言中的"strings"包,因为它提供了处理文本数据的工具,从基本的字符串拼接到高级的正则表达式匹配。1)"strings"包提供了高效的字符串操作,如Join函数用于拼接字符串,避免性能问题。2)它包含高级功能,如ContainsAny函数,用于检查字符串是否包含特定字符集。3)Replace函数用于替换字符串中的子串,需注意替换顺序和大小写敏感性。4)Split函数可以根据分隔符拆分字符串,常用于正则表达式处理。5)使用时需考虑性能,如

“编码/二进制”软件包interingoisentialForHandlingBinaryData,oferingToolSforreDingingAndWritingBinaryDataEfficely.1)Itsupportsbothlittle-endianandBig-endianBig-endianbyteorders,CompialforOss-System-System-System-compatibility.2)

掌握Go语言中的bytes包有助于提高代码的效率和优雅性。1)bytes包对于解析二进制数据、处理网络协议和内存管理至关重要。2)使用bytes.Buffer可以逐步构建字节切片。3)bytes包提供了搜索、替换和分割字节切片的功能。4)bytes.Reader类型适用于从字节切片读取数据,特别是在I/O操作中。5)bytes包与Go的垃圾回收器协同工作,提高了大数据处理的效率。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver Mac版
视觉化网页开发工具

Dreamweaver CS6
视觉化网页开发工具

SublimeText3汉化版
中文版,非常好用