使用chromedp启动具有特定用户配置文件的浏览器。

huangapple go评论121阅读模式
英文:

start browser with specicfic user profile using chromedp

问题

我正在使用Python的Selenium,并且可以使用它完成所有的测试工作。但是我正在学习Golang,并且想尝试使用它进行测试。我找到了chromedp(chromedp的GitHub仓库)并且很喜欢它,但是我无法弄清楚如何使用特定的用户配置文件启动Google Chrome。有人可以帮忙吗?

以下是你提供的示例代码的翻译:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "time"
  8. cdp "github.com/knq/chromedp"
  9. cdptypes "github.com/knq/chromedp/cdp"
  10. )
  11. func main() {
  12. var err error
  13. // 创建上下文
  14. ctxt, cancel := context.WithCancel(context.Background())
  15. defer cancel()
  16. // 创建 Chrome 实例
  17. c, err := cdp.New(ctxt, cdp.WithLog(log.Printf))
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. // 运行任务列表
  22. var site, res string
  23. err = c.Run(ctxt, googleSearch("site:brank.as", "Easy Money Management", &site, &res))
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27. // 关闭 Chrome
  28. err = c.Shutdown(ctxt)
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. // 等待 Chrome 完成
  33. err = c.Wait()
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. log.Printf("保存了搜索结果列表中 #testimonials 的截图 `%s` (%s)", res, site)
  38. }
  39. func googleSearch(q, text string, site, res *string) cdp.Tasks {
  40. var buf []byte
  41. sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text)
  42. return cdp.Tasks{
  43. cdp.Navigate(`https://www.google.com`),
  44. cdp.Sleep(2 * time.Second),
  45. cdp.WaitVisible(`#hplogo`, cdp.ByID),
  46. cdp.SendKeys(`#lst-ib`, q+"\n", cdp.ByID),
  47. cdp.WaitVisible(`#res`, cdp.ByID),
  48. cdp.Text(sel, res),
  49. cdp.Click(sel),
  50. cdp.Sleep(2 * time.Second),
  51. cdp.WaitVisible(`#footer`, cdp.ByQuery),
  52. cdp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, cdp.ByQuery),
  53. cdp.Location(site),
  54. cdp.Screenshot(`#testimonials`, &buf, cdp.ByID),
  55. cdp.ActionFunc(func(context.Context, cdptypes.Handler) error {
  56. return ioutil.WriteFile("testimonials.png", buf, 0644)
  57. }),
  58. }
  59. }

希望对你有帮助!

英文:

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 :

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "time"
  8. cdp "github.com/knq/chromedp"
  9. cdptypes "github.com/knq/chromedp/cdp"
  10. )
  11. func main() {
  12. var err error
  13. // create context
  14. ctxt, cancel := context.WithCancel(context.Background())
  15. defer cancel()
  16. // create chrome instance
  17. c, err := cdp.New(ctxt, cdp.WithLog(log.Printf))
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. // run task list
  22. var site, res string
  23. err = c.Run(ctxt, googleSearch("site:brank.as", "Easy Money Management", &site, &res))
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27. // shutdown chrome
  28. err = c.Shutdown(ctxt)
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. // wait for chrome to finish
  33. err = c.Wait()
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. log.Printf("saved screenshot of #testimonials from search result listing `%s` (%s)", res, site)
  38. }
  39. func googleSearch(q, text string, site, res *string) cdp.Tasks {
  40. var buf []byte
  41. sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text)
  42. return cdp.Tasks{
  43. cdp.Navigate(`https://www.google.com`),
  44. cdp.Sleep(2 * time.Second),
  45. cdp.WaitVisible(`#hplogo`, cdp.ByID),
  46. cdp.SendKeys(`#lst-ib`, q+"\n", cdp.ByID),
  47. cdp.WaitVisible(`#res`, cdp.ByID),
  48. cdp.Text(sel, res),
  49. cdp.Click(sel),
  50. cdp.Sleep(2 * time.Second),
  51. cdp.WaitVisible(`#footer`, cdp.ByQuery),
  52. cdp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, cdp.ByQuery),
  53. cdp.Location(site),
  54. cdp.Screenshot(`#testimonials`, &buf, cdp.ByID),
  55. cdp.ActionFunc(func(context.Context, cdptypes.Handler) error {
  56. return ioutil.WriteFile("testimonials.png", buf, 0644)
  57. }),
  58. }
  59. }

答案1

得分: 1

你需要使用以下的运行选项:

  1. cdp, err := cdp.New(ctxt, cdp.WithRunnerOptions(
  2. runner.UserDataDir("你的路径"),
  3. ))

你可以在下面的链接中查找所有可用的选项:

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

英文:

You need to use the runner options for this

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

You can look for all available options at below link

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

答案2

得分: 0

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

  1. opts := []chromedp.ExecAllocatorOption{
  2. chromedp.UserDataDir(`...\AppData\Local\Google\Chrome\User Data`),
  3. chromedp.Flag("profile-directory", "Profile 1"), // <-- 就像这样
  4. ...
英文:

When you have multiple 'profiles' in the user data directory you can also specify the name of the profile directory you want to use:

  1. opts := []chromedp.ExecAllocatorOption{
  2. chromedp.UserDataDir(`...\AppData\Local\Google\Chrome\User Data`),
  3. chromedp.Flag(&quot;profile-directory&quot;, &quot;Profile 1&quot;), // &lt;-- like this
  4. ...

huangapple
  • 本文由 发表于 2017年8月25日 05:39:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/45871304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定