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

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

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

问题

package main

import (
	"fmt"
	"time"

	"github.com/tebeka/selenium"
	"github.com/tebeka/selenium/chrome"
)

func main() {
	// 运行 Chrome 浏览器

	service, err := selenium.NewChromeDriverService("./chromedriver", 4444)
	if err != nil {
		panic(err)
	}
	defer service.Stop()

	caps := selenium.Capabilities{}

	caps.AddChrome(chrome.Capabilities{
		Path: "C:/Program Files/Google/Chrome/Application/chrome.exe",
		Args: []string{
			"window-size=1920x1080",
			"--no-sandbox",
			"--disable-dev-shm-usage",
			"--disable-gpu",
			"--user-data-dir=C:/Users/nikit/AppData/Local/Google/Chrome/User Data/Profile 2",

			// "--headless", // 注释掉此行以查看浏览器
		}})

	driver, err := selenium.NewRemote(caps, "")
	if err != nil {
		panic(err)
	}

	driver.Get("https://point.wb.ru/login")
	elem, _ := driver.FindElement(selenium.ByClassName, "opp-form")
	if elem != nil {
		fmt.Println("找到了!")
	}
	time.Sleep(time.Second * 100)
}

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

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

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

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

感谢任何帮助。

更新:

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

package main

import (
	"fmt"
	"time"

	"github.com/tebeka/selenium"
	"github.com/tebeka/selenium/chrome"
)

func main() {
	// 运行 Chrome 浏览器

	service, err := selenium.NewChromeDriverService("./chromedriver", 4444)
	if err != nil {
		panic(err)
	}
	defer service.Stop()

	caps := selenium.Capabilities{}

	caps.AddChrome(chrome.Capabilities{
		Path: "C:/Program Files/Google/Chrome/Application/chrome.exe",
		Args: []string{
			"window-size=1920x1080",
			"--no-sandbox",
			"--disable-dev-shm-usage",
			"--disable-gpu",
			"--user-data-dir=C:/Users/nikit/AppData/Local/Google/Chrome/User Data/Profile 2",

			// "--headless", // 注释掉此行以查看浏览器
		}})

	driver, err := selenium.NewRemote(caps, "")
	if err != nil {
		panic(err)
	}

	driver.Get("https://point.wb.ru/login")
	elem, _ := driver.FindElement(selenium.ByClassName, "opp-form")
	if elem != nil {
		fmt.Println("找到了!")
	}
	time.Sleep(time.Second * 100)
}

希望对你有帮助!

英文:
package main

import (
	"fmt"
	"time"

	"github.com/tebeka/selenium"
	"github.com/tebeka/selenium/chrome"
)

func main() {
	// Run Chrome browser
	
	service, err := selenium.NewChromeDriverService("./chromedriver", 4444)
	if err != nil {
		panic(err)
	}
	defer service.Stop()

	caps := selenium.Capabilities{}
	caps.AddChrome(chrome.Capabilities{Args: []string{
		"window-size=1920x1080",
		"--no-sandbox",
		"--disable-dev-shm-usage",
		"disable-gpu",
		// "--headless", // comment out this line to see the browser
	}})
	
	driver, err := selenium.NewRemote(caps, "")
	if err != nil {
		panic(err)
	}

	driver.Get("https://google.com")
	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

package main

import (
	"fmt"
	"time"

	"github.com/tebeka/selenium"
	"github.com/tebeka/selenium/chrome"
)

func main() {
	// Run Chrome browser

	service, err := selenium.NewChromeDriverService("./chromedriver", 4444)
	if err != nil {
		panic(err)
	}
	defer service.Stop()

	caps := selenium.Capabilities{}

	caps.AddChrome(chrome.Capabilities{
		Path: "C:/Program Files/Google/Chrome/Application/chrome.exe",
		Args: []string{
			"window-size=1920x1080",
			"--no-sandbox",
			"--disable-dev-shm-usage",
			"--disable-gpu",
			"--user-data-dir=C:/Users/nikit/AppData/Local/Google/Chrome/User Data/Profile 2",

			// "--headless", // comment out this line to see the browser
		}})

	driver, err := selenium.NewRemote(caps, "")
	if err != nil {
		panic(err)
	}

	driver.Get("https://point.wb.ru/login")
	elem, _ := driver.FindElement(selenium.ByClassName, "opp-form")
	if elem != nil {
		fmt.Println("нашел!")
	}
	time.Sleep(time.Second * 100)
}

答案1

得分: 0

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

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

nevermind i did it...

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

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:

确定