【问题标题】:start browser with specicfic user profile using chromedp使用 chromedp 启动具有特定用户配置文件的浏览器
【发布时间】:2021-07-07 20:39:54
【问题描述】:

我在 python 中使用 selenium,我可以使用它完成所有测试工作 但我正在学习 Golang,我想尝试使用它进行测试 我碰到 chromedp the chromedp github repo 我喜欢它,但是 我不知道如何使用特定的用户配置文件启动 google chrome

有人可以帮忙吗?

我正在使用这个例子:

    package main

import (
    "context"
    "fmt"
    "io/ioutil"
    "log"
    "time"

    cdp "github.com/knq/chromedp"
    cdptypes "github.com/knq/chromedp/cdp"
)

func main() {
    var err error

    // create context
    ctxt, cancel := context.WithCancel(context.Background())
    defer cancel()

    // create chrome instance
    c, err := cdp.New(ctxt, cdp.WithLog(log.Printf))
    if err != nil {
        log.Fatal(err)
    }

    // run task list
    var site, res string
    err = c.Run(ctxt, googleSearch("site:brank.as", "Easy Money Management", &site, &res))
    if err != nil {
        log.Fatal(err)
    }

    // shutdown chrome
    err = c.Shutdown(ctxt)
    if err != nil {
        log.Fatal(err)
    }

    // wait for chrome to finish
    err = c.Wait()
    if err != nil {
        log.Fatal(err)
    }

    log.Printf("saved screenshot of #testimonials from search result listing `%s` (%s)", res, site)
}

func googleSearch(q, text string, site, res *string) cdp.Tasks {
    var buf []byte
    sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text)
    return cdp.Tasks{
        cdp.Navigate(`https://www.google.com`),
        cdp.Sleep(2 * time.Second),
        cdp.WaitVisible(`#hplogo`, cdp.ByID),
        cdp.SendKeys(`#lst-ib`, q+"\n", cdp.ByID),
        cdp.WaitVisible(`#res`, cdp.ByID),
        cdp.Text(sel, res),
        cdp.Click(sel),
        cdp.Sleep(2 * time.Second),
        cdp.WaitVisible(`#footer`, cdp.ByQuery),
        cdp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, cdp.ByQuery),
        cdp.Location(site),
        cdp.Screenshot(`#testimonials`, &buf, cdp.ByID),
        cdp.ActionFunc(func(context.Context, cdptypes.Handler) error {
            return ioutil.WriteFile("testimonials.png", buf, 0644)
        }),
    }
}

【问题讨论】:

    标签: google-chrome selenium testing go browser-automation


    【解决方案1】:

    您需要为此使用运行器选项

    cdp, err := cdp.New(ctxt, cdp.WithRunnerOptions(
                runner.UserDataDir("<your path>"),
            ))
    

    您可以在以下链接中查找所有可用选项

    https://github.com/knq/chromedp/blob/dc08ecc7272dd745adc3494fb675c76174cbb2b3/runner/runner.go

    【讨论】:

    • 我试过了,但它所做的只是在我的浏览器中打开一个新的水龙头,然后打印一个超时错误。你能给我一个linux中用户路径配置文件的例子吗?也许我做错了什么。我的就像 /home/.config/google-chrome/Default/
    • 你能杀掉所有的chrome进程然后试试吗?
    • 我做了,我发现它创建了我给我的源程序文件夹的目录,但仍然没有打开我的个人资料
    • 您使用的是什么路径?您想使用现有配置文件启动它吗?
    • 我指向我的 google-chrome 配置文件夹中的配置文件 1,这是错误的,我将它指向了 google chrome 的根目录,它运行良好。谢谢你的帮助
    【解决方案2】:

    当您在用户数据目录中有多个“配置文件”时,您还可以指定要使用的配置文件目录的名称:

    opts := []chromedp.ExecAllocatorOption{
            chromedp.UserDataDir(`...\AppData\Local\Google\Chrome\User Data`),
            chromedp.Flag("profile-directory", "Profile 1"), // <-- like this
            ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多