英文:
start browser with specicfic user profile using chromedp
问题
我正在使用Python的Selenium,并且可以使用它完成所有的测试工作。但是我正在学习Golang,并且想尝试使用它进行测试。我找到了chromedp(chromedp的GitHub仓库)并且很喜欢它,但是我无法弄清楚如何使用特定的用户配置文件启动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
// 创建上下文
ctxt, cancel := context.WithCancel(context.Background())
defer cancel()
// 创建 Chrome 实例
c, err := cdp.New(ctxt, cdp.WithLog(log.Printf))
if err != nil {
log.Fatal(err)
}
// 运行任务列表
var site, res string
err = c.Run(ctxt, googleSearch("site:brank.as", "Easy Money Management", &site, &res))
if err != nil {
log.Fatal(err)
}
// 关闭 Chrome
err = c.Shutdown(ctxt)
if err != nil {
log.Fatal(err)
}
// 等待 Chrome 完成
err = c.Wait()
if err != nil {
log.Fatal(err)
}
log.Printf("保存了搜索结果列表中 #testimonials 的截图 `%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)
}),
}
}
希望对你有帮助!
英文:
i am using selenium with python and i can do all my testing work using it
but i am learning Golang and i want to try to test using it
i came across
chromedp the chromedp github repo
and i like it but
i couldn't figure out how to start google chrome with a specific user profile
can any one help please ?
i am using this example :
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)
}),
}
}
答案1
得分: 1
你需要使用以下的运行选项:
cdp, err := cdp.New(ctxt, cdp.WithRunnerOptions(
runner.UserDataDir("你的路径"),
))
你可以在下面的链接中查找所有可用的选项:
https://github.com/knq/chromedp/blob/dc08ecc7272dd745adc3494fb675c76174cbb2b3/runner/runner.go
英文:
You need to use the runner options for this
cdp, err := cdp.New(ctxt, cdp.WithRunnerOptions(
runner.UserDataDir("<your path>"),
))
You can look for all available options at below link
https://github.com/knq/chromedp/blob/dc08ecc7272dd745adc3494fb675c76174cbb2b3/runner/runner.go
答案2
得分: 0
当您在用户数据目录中有多个"profiles"时,您还可以指定要使用的配置文件目录的名称:
opts := []chromedp.ExecAllocatorOption{
chromedp.UserDataDir(`...\AppData\Local\Google\Chrome\User Data`),
chromedp.Flag("profile-directory", "Profile 1"), // <-- 就像这样
...
英文:
When you have multiple 'profiles' in the user data directory you can also specify the name of the profile directory you want to use:
opts := []chromedp.ExecAllocatorOption{
chromedp.UserDataDir(`...\AppData\Local\Google\Chrome\User Data`),
chromedp.Flag("profile-directory", "Profile 1"), // <-- like this
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论