how to launch my own browser version with selenium using go?

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

how to launch my own browser version with selenium using go?

问题

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/tebeka/selenium"
  6. "github.com/tebeka/selenium/chrome"
  7. )
  8. func main() {
  9. // 运行 Chrome 浏览器
  10. service, err := selenium.NewChromeDriverService("./chromedriver", 4444)
  11. if err != nil {
  12. panic(err)
  13. }
  14. defer service.Stop()
  15. caps := selenium.Capabilities{}
  16. caps.AddChrome(chrome.Capabilities{
  17. Path: "C:/Program Files/Google/Chrome/Application/chrome.exe",
  18. Args: []string{
  19. "window-size=1920x1080",
  20. "--no-sandbox",
  21. "--disable-dev-shm-usage",
  22. "--disable-gpu",
  23. "--user-data-dir=C:/Users/nikit/AppData/Local/Google/Chrome/User Data/Profile 2",
  24. // "--headless", // 注释掉此行以查看浏览器
  25. }})
  26. driver, err := selenium.NewRemote(caps, "")
  27. if err != nil {
  28. panic(err)
  29. }
  30. driver.Get("https://point.wb.ru/login")
  31. elem, _ := driver.FindElement(selenium.ByClassName, "opp-form")
  32. if elem != nil {
  33. fmt.Println("找到了!")
  34. }
  35. time.Sleep(time.Second * 100)
  36. }

以上代码创建了一个新的 Chrome 浏览器实例。在我的情况下,我需要打开我的默认浏览器,并保留我的 cookie 和其他数据(不需要选项卡)。

我没有找到使用 Go 编程语言的方法。我在这里找到了一个使用 C# 语言的解决方案(我猜是这样的):

  1. ChromeOptions options = new ChromeOptions();
  2. options.setBinary("/path/to/other/chrome/binary");

但是我是新手,不知道如何将其转换为适合我的代码。

感谢任何帮助。

更新:

我尝试了这个方法,但是它仍然无法工作 :c

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/tebeka/selenium"
  6. "github.com/tebeka/selenium/chrome"
  7. )
  8. func main() {
  9. // 运行 Chrome 浏览器
  10. service, err := selenium.NewChromeDriverService("./chromedriver", 4444)
  11. if err != nil {
  12. panic(err)
  13. }
  14. defer service.Stop()
  15. caps := selenium.Capabilities{}
  16. caps.AddChrome(chrome.Capabilities{
  17. Path: "C:/Program Files/Google/Chrome/Application/chrome.exe",
  18. Args: []string{
  19. "window-size=1920x1080",
  20. "--no-sandbox",
  21. "--disable-dev-shm-usage",
  22. "--disable-gpu",
  23. "--user-data-dir=C:/Users/nikit/AppData/Local/Google/Chrome/User Data/Profile 2",
  24. // "--headless", // 注释掉此行以查看浏览器
  25. }})
  26. driver, err := selenium.NewRemote(caps, "")
  27. if err != nil {
  28. panic(err)
  29. }
  30. driver.Get("https://point.wb.ru/login")
  31. elem, _ := driver.FindElement(selenium.ByClassName, "opp-form")
  32. if elem != nil {
  33. fmt.Println("找到了!")
  34. }
  35. time.Sleep(time.Second * 100)
  36. }

希望对你有帮助!

英文:
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/tebeka/selenium"
  6. "github.com/tebeka/selenium/chrome"
  7. )
  8. func main() {
  9. // Run Chrome browser
  10. service, err := selenium.NewChromeDriverService("./chromedriver", 4444)
  11. if err != nil {
  12. panic(err)
  13. }
  14. defer service.Stop()
  15. caps := selenium.Capabilities{}
  16. caps.AddChrome(chrome.Capabilities{Args: []string{
  17. "window-size=1920x1080",
  18. "--no-sandbox",
  19. "--disable-dev-shm-usage",
  20. "disable-gpu",
  21. // "--headless", // comment out this line to see the browser
  22. }})
  23. driver, err := selenium.NewRemote(caps, "")
  24. if err != nil {
  25. panic(err)
  26. }
  27. driver.Get("https://google.com")
  28. time.Sleep(50e+10)

The code above makes a new clear version of chrome browser. In my case I need to open my default one with my cookies and other data (no tabs needed).

I haven't found a method using Go programming language. I found a solution for a C# language (i guess) here it is
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");

but I'm noob and i don't know how to convert it to fit in my code.

Thanks for any help

UPDATE

I tryed this. But it doesn't work anyway :c

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/tebeka/selenium"
  6. "github.com/tebeka/selenium/chrome"
  7. )
  8. func main() {
  9. // Run Chrome browser
  10. service, err := selenium.NewChromeDriverService("./chromedriver", 4444)
  11. if err != nil {
  12. panic(err)
  13. }
  14. defer service.Stop()
  15. caps := selenium.Capabilities{}
  16. caps.AddChrome(chrome.Capabilities{
  17. Path: "C:/Program Files/Google/Chrome/Application/chrome.exe",
  18. Args: []string{
  19. "window-size=1920x1080",
  20. "--no-sandbox",
  21. "--disable-dev-shm-usage",
  22. "--disable-gpu",
  23. "--user-data-dir=C:/Users/nikit/AppData/Local/Google/Chrome/User Data/Profile 2",
  24. // "--headless", // comment out this line to see the browser
  25. }})
  26. driver, err := selenium.NewRemote(caps, "")
  27. if err != nil {
  28. panic(err)
  29. }
  30. driver.Get("https://point.wb.ru/login")
  31. elem, _ := driver.FindElement(selenium.ByClassName, "opp-form")
  32. if elem != nil {
  33. fmt.Println("нашел!")
  34. }
  35. time.Sleep(time.Second * 100)
  36. }

答案1

得分: 0

不用管了,我已经做完了...

  1. caps.AddChrome(chrome.Capabilities{
  2. Path: "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
  3. Args: []string{
  4. "start-maximized",
  5. "--no-sandbox",
  6. "--disable-dev-shm-usage",
  7. "--disable-gpu",
  8. "--user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data",
  9. "--profile-directory=Profile Default",
  10. // "--headless", // 注释掉这一行可以看到浏览器
  11. },
  12. })
英文:

nevermind i did it...

  1. caps.AddChrome(chrome.Capabilities{
  2. Path: "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
  3. Args: []string{
  4. "start-maximized",
  5. "--no-sandbox",
  6. "--disable-dev-shm-usage",
  7. "--disable-gpu",
  8. "--user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data",
  9. "--profile-directory=Profile Default",
  10. // "--headless", // comment out this lienter code herene to see the browser
  11. }})

huangapple
  • 本文由 发表于 2023年2月11日 21:13:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75420439.html
匿名

发表评论

匿名网友

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

确定