英文:
How to run different codes according to login status in chromedp?
问题
演示代码:
package main
import (
"context"
"log"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/kb"
)
func getTargetId(ctx context.Context, title string) (targetId target.ID) {
targets, err := chromedp.Targets(ctx)
if err != nil {
log.Fatalln(err)
}
for _, t := range targets {
// log.Printf("title %s , ID %s \n", t.Title, t.TargetID)
if title == t.Title {
return t.TargetID
}
}
return ""
}
func main() {
options := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("headless", false),
chromedp.DisableGPU,
chromedp.Flag("disable-extensions", false),
chromedp.Flag("enable-automation", false),
chromedp.UserAgent("Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"),
)
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), options...)
defer cancel()
task1Ctx, cancel := chromedp.NewContext(allocCtx)
defer cancel()
if err := chromedp.Run(task1Ctx,
chromedp.Navigate("https://stackoverflow.com/questions/69672682/how-to-run-different-codes-according-to-login-status-in-chromedp"),
); err != nil {
log.Printf("navigate stackoverflow.com err : %v\n", err)
}
task2Ctx, cancel := chromedp.NewContext(task1Ctx)
defer cancel()
var id []cdp.NodeID
//add timeout 10 seconds
tCtx, cancel := context.WithTimeout(task2Ctx, 10*time.Second)
defer cancel()
if err := chromedp.Run(tCtx,
chromedp.Navigate("https://github.com/chromedp/chromedp"),
//user info button upper right corner
chromedp.NodeIDs(`body > div.position-relative.js-header-wrapper > header > div.Header-item.position-relative.mr-0.d-none.d-md-flex`, &id, chromedp.ByQuery),
); err != nil {
log.Printf("whether need login err : %v\n", err)
}
//get targetid by title
id_task2Ctx := getTargetId(task1Ctx, "GitHub - chromedp/chromedp: A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol.")
//return to the tab
task2Ctx, cancel = chromedp.NewContext(task1Ctx, chromedp.WithTargetID(id_task2Ctx))
defer cancel()
if err := chromedp.Run(task2Ctx,
chromedp.Navigate("https://github.com/chromedp/chromedp"),
); err != nil {
log.Printf("navigate chromedp err : %v\n", err)
}
//determine whether login is required
if len(id) == 0 {
// code for login
if err := chromedp.Run(task2Ctx,
//click sign in
chromedp.Click(`body > div.position-relative.js-header-wrapper > header > div > div.HeaderMenu.HeaderMenu--logged-out.position-fixed.top-0.right-0.bottom-0.height-fit.position-lg-relative.d-lg-flex.flex-justify-between.flex-items-center.flex-auto > div.d-lg-flex.flex-items-center.px-3.px-lg-0.text-center.text-lg-left > div.position-relative.mr-3.mb-4.mb-lg-0.d-inline-block > a`, chromedp.ByQuery),
chromedp.SendKeys(`#login_field`, `XXX`),
chromedp.SendKeys(`#password`, `XXX`+kb.Enter),
); err != nil {
log.Printf("login err : %v\n", err)
}
}
if err := chromedp.Run(task2Ctx,
//click master
chromedp.Click(`#branch-select-menu > summary`, chromedp.ByQuery),
//click tag
chromedp.Click(`#branch-select-menu > div > div > input-demux > tab-container > div.SelectMenu-tabs > button:nth-child(2)`, chromedp.ByQuery),
//wait tags list visible
chromedp.WaitVisible(`#tags-menu > ref-selector > div > div > div > a:nth-child(1)`, chromedp.ByQuery),
//click v0.6.6
chromedp.Click(`//*[@id="tags-menu"]/ref-selector/div/div/div/a/span[text()="v0.6.6"]`, chromedp.BySearch),
chromedp.Sleep(20*time.Second),
); err != nil {
log.Printf("logined err : %v\n", err)
}
}
我期望看到什么?
1、如果没有登录,它将点击“登录”并进行登录,然后点击“master”>“Tags”>“v0.6.6”。
2、如果已登录,它将点击“master”>“Tags”>“v0.6.6”。
实际看到了什么?
1、如果没有登录,运行代码时,有时会出现错误:
2021/10/22 12:45:42 login err : Could not find node with given id (-32000)
2、如果已登录,运行代码时,会打开两个“https://github.com/chromedp/chromedp”的标签页。
英文:
demo codes:
package main
import (
"context"
"log"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/kb"
)
func getTargetId(ctx context.Context, title string) (targetId target.ID) {
targets, err := chromedp.Targets(ctx)
if err != nil {
log.Fatalln(err)
}
for _, t := range targets {
// log.Printf("title %s , ID %s \n", t.Title, t.TargetID)
if title == t.Title {
return t.TargetID
}
}
return ""
}
func main() {
options := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag(`headless`, false),
chromedp.DisableGPU,
chromedp.Flag(`disable-extensions`, false),
chromedp.Flag(`enable-automation`, false),
chromedp.UserAgent(`Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36`),
)
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), options...)
defer cancel()
task1Ctx, cancel := chromedp.NewContext(allocCtx)
defer cancel()
if err := chromedp.Run(task1Ctx,
chromedp.Navigate("https://stackoverflow.com/questions/69672682/how-to-run-different-codes-according-to-login-status-in-chromedp"),
); err != nil {
log.Printf("navigate stackoverflow.com err : %v\n", err)
}
task2Ctx, cancel := chromedp.NewContext(task1Ctx)
defer cancel()
var id []cdp.NodeID
//add timeout 10 seconds
tCtx, cancel := context.WithTimeout(task2Ctx, 10*time.Second)
defer cancel()
if err := chromedp.Run(tCtx,
chromedp.Navigate("https://github.com/chromedp/chromedp"),
//user info button upper right corner
chromedp.NodeIDs(`body > div.position-relative.js-header-wrapper > header > div.Header-item.position-relative.mr-0.d-none.d-md-flex`, &id, chromedp.ByQuery),
); err != nil {
log.Printf("whether need login err : %v\n", err)
}
//get targetid by title
id_task2Ctx := getTargetId(task1Ctx, "GitHub - chromedp/chromedp: A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol.")
//return to the tab
task2Ctx, cancel = chromedp.NewContext(task1Ctx, chromedp.WithTargetID(id_task2Ctx))
defer cancel()
if err := chromedp.Run(task2Ctx,
chromedp.Navigate("https://github.com/chromedp/chromedp"),
); err != nil {
log.Printf("navigate chromedp err : %v\n", err)
}
//determine whether login is required
if len(id) == 0 {
// code for login
if err := chromedp.Run(task2Ctx,
//click sign in
chromedp.Click(`body > div.position-relative.js-header-wrapper > header > div > div.HeaderMenu.HeaderMenu--logged-out.position-fixed.top-0.right-0.bottom-0.height-fit.position-lg-relative.d-lg-flex.flex-justify-between.flex-items-center.flex-auto > div.d-lg-flex.flex-items-center.px-3.px-lg-0.text-center.text-lg-left > div.position-relative.mr-3.mb-4.mb-lg-0.d-inline-block > a`, chromedp.ByQuery),
chromedp.SendKeys(`#login_field`, `XXX`),
chromedp.SendKeys(`#password`, `XXX`+kb.Enter),
); err != nil {
log.Printf("login err : %v\n", err)
}
}
if err := chromedp.Run(task2Ctx,
//click master
chromedp.Click(`#branch-select-menu > summary`, chromedp.ByQuery),
//click tag
chromedp.Click(`#branch-select-menu > div > div > input-demux > tab-container > div.SelectMenu-tabs > button:nth-child(2)`, chromedp.ByQuery),
//wait tags list visible
chromedp.WaitVisible(`#tags-menu > ref-selector > div > div > div > a:nth-child(1)`, chromedp.ByQuery),
//click v0.6.6
chromedp.Click(`//*[@id="tags-menu"]/ref-selector/div/div/div/a/span[text()="v0.6.6"]`, chromedp.BySearch),
chromedp.Sleep(20*time.Second),
); err != nil {
log.Printf("logined err : %v\n", err)
}
}
What did I expect to see?
1、If it is not logined, it will click sign in and login, then click master > Tags > v0.6.6
2、If it is logined, it will click master > Tags > v0.6.6
What did I see instead?
1、If it is not logined, run the code, sometimes it get errors:
2021/10/22 12:45:42 login err : Could not find node with given id (-32000)
2、If it is logined, run the code, it opens two tabs of "https://github.com/chromedp/chromedp"
答案1
得分: 1
package main
import (
"context"
"log"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/kb"
)
func getTargetId(ctx context.Context, title string) (targetId target.ID) {
targets, err := chromedp.Targets(ctx)
if err != nil {
log.Fatalln(err)
}
for _, t := range targets {
// log.Printf("title %s , ID %s \n", t.Title, t.TargetID)
if title == t.Title {
return t.TargetID
}
}
return ""
}
func main() {
options := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag(`headless`, false),
chromedp.DisableGPU,
chromedp.Flag(`disable-extensions`, false),
chromedp.Flag(`enable-automation`, false),
chromedp.UserAgent(`Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36`),
)
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), options...)
defer cancel()
// task1Ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithDebugf(log.Printf))
task1Ctx, cancel := chromedp.NewContext(allocCtx)
defer cancel()
if err := chromedp.Run(task1Ctx,
chromedp.Navigate("https://stackoverflow.com/questions/69672682/how-to-run-different-codes-according-to-login-status-in-chromedp"),
); err != nil {
log.Printf("navigate stackoverflow.com err : %v\n", err)
}
task2Ctx, cancel := chromedp.NewContext(task1Ctx)
defer cancel()
var id []cdp.NodeID
//add timeout 15 seconds
task2Ctx, cancel = context.WithTimeout(task2Ctx, 15*time.Second)
defer cancel()
if err := chromedp.Run(task2Ctx,
chromedp.Navigate("https://github.com/chromedp/chromedp"),
//user info button upper right corner
chromedp.NodeIDs(`body > div.position-relative.js-header-wrapper > header > div.Header-item.position-relative.mr-0.d-none.d-md-flex`, &id, chromedp.ByQuery),
); err != nil {
log.Printf("whether need login err : %v\n", err)
}
//get targetid by title, task2Ctx stopped, get targetid from task1Ctx only!
id_task2Ctx := getTargetId(task1Ctx, "GitHub - chromedp/chromedp: A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol.")
//determine whether login is required
if len(id) == 0 {
//return to the tab
task2Ctx, cancel = chromedp.NewContext(task1Ctx, chromedp.WithTargetID(id_task2Ctx))
defer cancel()
// code for login
if err := chromedp.Run(task2Ctx,
//click sign in
chromedp.Click(`body > div.position-relative.js-header-wrapper > header > div > div.HeaderMenu.HeaderMenu--logged-out.position-fixed.top-0.right-0.bottom-0.height-fit.position-lg-relative.d-lg-flex.flex-justify-between.flex-items-center.flex-auto > div.d-lg-flex.flex-items-center.px-3.px-lg-0.text-center.text-lg-left > div.position-relative.mr-3.mb-4.mb-lg-0.d-inline-block > a`, chromedp.ByQuery),
chromedp.SendKeys(`#login_field`, `XXX`),
chromedp.SendKeys(`#password`, `XXX`+kb.Enter),
); err != nil {
log.Printf("login err : %v\n", err)
}
}
if err := chromedp.Run(task2Ctx,
//click master
chromedp.Click(`#branch-select-menu > summary`, chromedp.ByQuery),
//wait Tags visible
chromedp.WaitVisible(`#branch-select-menu > div > div > input-demux > tab-container > div.SelectMenu-tabs > button:nth-child(2)`, chromedp.ByQuery),
//click Tags
chromedp.Click(`#branch-select-menu > div > div > input-demux > tab-container > div.SelectMenu-tabs > button:nth-child(2)`, chromedp.ByQuery),
//wait tags-list child visible
chromedp.WaitVisible(`#tags-menu > ref-selector > div > div > div > a:nth-child(1)`, chromedp.ByQuery),
//click v0.6.6
chromedp.Click(`//*[@id="tags-menu"]/ref-selector/div/div/div/a/span[text()="v0.6.6"]`, chromedp.BySearch),
chromedp.Sleep(15*time.Second),
); err != nil {
log.Printf("logined err : %v\n", err)
}
}
英文:
package main
import (
"context"
"log"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/kb"
)
func getTargetId(ctx context.Context, title string) (targetId target.ID) {
targets, err := chromedp.Targets(ctx)
if err != nil {
log.Fatalln(err)
}
for _, t := range targets {
// log.Printf("title %s , ID %s \n", t.Title, t.TargetID)
if title == t.Title {
return t.TargetID
}
}
return ""
}
func main() {
options := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag(`headless`, false),
chromedp.DisableGPU,
chromedp.Flag(`disable-extensions`, false),
chromedp.Flag(`enable-automation`, false),
chromedp.UserAgent(`Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36`),
)
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), options...)
defer cancel()
// task1Ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithDebugf(log.Printf))
task1Ctx, cancel := chromedp.NewContext(allocCtx)
defer cancel()
if err := chromedp.Run(task1Ctx,
chromedp.Navigate("https://stackoverflow.com/questions/69672682/how-to-run-different-codes-according-to-login-status-in-chromedp"),
); err != nil {
log.Printf("navigate stackoverflow.com err : %v\n", err)
}
task2Ctx, cancel := chromedp.NewContext(task1Ctx)
defer cancel()
var id []cdp.NodeID
//add timeout 15 seconds
task2Ctx, cancel = context.WithTimeout(task2Ctx, 15*time.Second)
defer cancel()
if err := chromedp.Run(task2Ctx,
chromedp.Navigate("https://github.com/chromedp/chromedp"),
//user info button upper right corner
chromedp.NodeIDs(`body > div.position-relative.js-header-wrapper > header > div.Header-item.position-relative.mr-0.d-none.d-md-flex`, &id, chromedp.ByQuery),
); err != nil {
log.Printf("whether need login err : %v\n", err)
}
//get targetid by title, task2Ctx stopped, get targetid from task1Ctx only!
id_task2Ctx := getTargetId(task1Ctx, "GitHub - chromedp/chromedp: A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol.")
//determine whether login is required
if len(id) == 0 {
//return to the tab
task2Ctx, cancel = chromedp.NewContext(task1Ctx, chromedp.WithTargetID(id_task2Ctx))
defer cancel()
// code for login
if err := chromedp.Run(task2Ctx,
//click sign in
chromedp.Click(`body > div.position-relative.js-header-wrapper > header > div > div.HeaderMenu.HeaderMenu--logged-out.position-fixed.top-0.right-0.bottom-0.height-fit.position-lg-relative.d-lg-flex.flex-justify-between.flex-items-center.flex-auto > div.d-lg-flex.flex-items-center.px-3.px-lg-0.text-center.text-lg-left > div.position-relative.mr-3.mb-4.mb-lg-0.d-inline-block > a`, chromedp.ByQuery),
chromedp.SendKeys(`#login_field`, `XXX`),
chromedp.SendKeys(`#password`, `XXX`+kb.Enter),
); err != nil {
log.Printf("login err : %v\n", err)
}
}
if err := chromedp.Run(task2Ctx,
//click master
chromedp.Click(`#branch-select-menu > summary`, chromedp.ByQuery),
//wait Tags visible
chromedp.WaitVisible(`#branch-select-menu > div > div > input-demux > tab-container > div.SelectMenu-tabs > button:nth-child(2)`, chromedp.ByQuery),
//click Tags
chromedp.Click(`#branch-select-menu > div > div > input-demux > tab-container > div.SelectMenu-tabs > button:nth-child(2)`, chromedp.ByQuery),
//wait tags-list child visible
chromedp.WaitVisible(`#tags-menu > ref-selector > div > div > div > a:nth-child(1)`, chromedp.ByQuery),
//click v0.6.6
chromedp.Click(`//*[@id="tags-menu"]/ref-selector/div/div/div/a/span[text()="v0.6.6"]`, chromedp.BySearch),
chromedp.Sleep(15*time.Second),
); err != nil {
log.Printf("logined err : %v\n", err)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论