我在python中使用selenium,我可以使用它完成所有的测试工作,但是我正在学习Golang,我想尝试使用它进行测试,我偶然遇到了chromedp 铬化github回购,我喜欢它,但是我不知道如何用特定的用户配置文件启动google。
有谁能帮忙吗?
我用的是这个例子:
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)
}),
}
}
发布于 2017-08-25 11:34:28
为此,需要使用runner选项。
cdp, err := cdp.New(ctxt, cdp.WithRunnerOptions(
runner.UserDataDir("<your path>"),
))
您可以在下面的链接中查找所有可用选项。
https://github.com/knq/chromedp/blob/dc08ecc7272dd745adc3494fb675c76174cbb2b3/runner/runner.go
发布于 2021-07-07 12:39:54
当用户数据目录中有多个“配置文件”时,还可以指定要使用的配置文件目录的名称:
opts := []chromedp.ExecAllocatorOption{
chromedp.UserDataDir(`...\AppData\Local\Google\Chrome\User Data`),
chromedp.Flag("profile-directory", "Profile 1"), // <-- like this
...
https://stackoverflow.com/questions/45871304
复制相似问题