如何在使用golang的远程selenium中加载Chrome扩展程序?

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

How to load extension for Chrome in remote selenium using golang?

问题

我在docker容器中使用了selenium(来自aerocube的selenoid)和golang的selenium库(来自tebeka)。

我找不到任何示例展示如何在带有扩展的远程selenium中运行chrome(仅限于go)。

我甚至在库中找到了一个可以实现的函数,但我找不到使用它的代码示例。
(https://pkg.go.dev/github.com/tebeka/selenium@v0.9.9/chrome#Capabilities.AddExtension)

caps := selenium.Capabilities{"browserName": "chrome", "browserVersion": "103.0"}

driver, err := selenium.NewRemote(caps, "http://127.0.0.1:4444/wd/hub")
if err != nil {
	fmt.Printf("create selenium session error: %v\n", err)
	return
}
defer driver.Quit()
driver.Get("https://www.google.com/")
driver.Close() 
英文:

I have selenium in docker container(selenoid from aerocube) and selenium library for golang from tebeka.

I can't find any examples that show how to run chrome in a remote selenium with the extension (literally only for go)

I even found function in library which do it, but I did not found example of code where It was used.
(https://pkg.go.dev/github.com/tebeka/selenium@v0.9.9/chrome#Capabilities.AddExtension)

caps := selenium.Capabilities{"browserName": "chrome", "browserVersion": "103.0"}

driver, err := selenium.NewRemote(caps, "http://127.0.0.1:4444/wd/hub")
if err != nil {
	fmt.Printf("create selenium session error: %v\n", err)
	return
}
defer driver.Quit()
driver.Get("https://www.google.com/")
driver.Close() 

答案1

得分: 2

我想使用modheader扩展,但是我遇到了同样的问题,我已经解决了。

步骤1:获取你的Chrome扩展(.crx)文件

在我的情况下,我找到了modheader文档,并从页面上获取了下载链接。

https://docs.modheader.com/advanced/selenium-webdriver

将.crx文件下载到你的项目中。
.crx下载页面

下载链接
https://github.com/modheader/modheader_selenium/raw/main/chrome-modheader/modheader.crx

注意:Web浏览器可能会根据策略阻止下载,使用"wget"命令获取文件。

浏览器阻止下载

wget下载文件

如果你想获取其他扩展,可以使用以下CRX Extracti/Downloader帮助你。

CRX Extracti链接
CRX Extracti/Downloader网页

步骤2:使用代码加载扩展

package main

import (
	"fmt"
	"os"

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

const (
	port = 8080
)

func main() {

	opts := []selenium.ServiceOption{
		// 启用虚拟XWindow会话。
		// selenium.StartFrameBuffer(),
		selenium.Output(os.Stderr), // 将调试信息输出到STDERR
	}

	_, err := selenium.NewChromeDriverService("../your_driver_path/chromedriver.exe", port, opts...)
	if err != nil {
		panic(err)
	}
	
	caps := selenium.Capabilities{"browserName": "chrome"}
	var cap_ext chrome.Capabilities

	// 通过crx文件添加你的扩展
	cap_ext.AddExtension("./modheader.crx")
	caps.AddChrome(cap_ext)

	wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://127.0.0.1:%d/wd/hub", port))

	// 使用API设置modheader

	// 添加header
	wd.Get("https://webdriver.modheader.com/add?test=ModHeader%20Test")

}

步骤3:设置扩展

Modheader扩展提供了API进行设置。

示例:

wd.Get("https://webdriver.modheader.com/add?test=ModHeader%20Test")

步骤4:结果

加载和设置扩展成功

希望能对你有所帮助。祝你好运。

英文:

I want to use the modheader extention, but I get the same question, and i solved it.

Step 1: Get your chrome extention (.crx) file

In my case, i find the modheader document,and get download link from the page.

https://docs.modheader.com/advanced/selenium-webdriver

Download the .crx file to you project.
the .crx download page

Download link
https://github.com/modheader/modheader_selenium/raw/main/chrome-modheader/modheader.crx

Note: Web browser maybe block the download by policy, use the "wget" command get the
file.

block by browser

wget the file

If you want to get other extention , use the follow CRX Extracti/Downloader can help you.

CRX Extracti link
CRX Extracti/Downloader webpage

Step 2: Use the Code loding the extention

package main

import (
	"fmt"
	"os"

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

const (
	port = 8080
)

func main() {

	opts := []selenium.ServiceOption{
		// Enable fake XWindow session.
		// selenium.StartFrameBuffer(),
		selenium.Output(os.Stderr), // Output debug information to STDERR
	}

	_, err := selenium.NewChromeDriverService("../your_driver_path/chromedriver.exe", port, opts...)
	if err != nil {
		panic(err)
	}
	
	caps := selenium.Capabilities{"browserName": "chrome"}
	var cap_ext chrome.Capabilities

	// add your extention by crx file
	cap_ext.AddExtension("./modheader.crx")
	caps.AddChrome(cap_ext)

	wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://127.0.0.1:%d/wd/hub", port))

	// Using api to setting modheader

	// add header
	wd.Get("https://webdriver.modheader.com/add?test=ModHeader%20Test")

}

Step 3: Setting the extention

Modheader extention supply api to setting.

Example :

wd.Get("https://webdriver.modheader.com/add?test=ModHeader%20Test")

Step 4: Result

Loading and setting extention successful

I hope that I can help. Good Luck.

huangapple
  • 本文由 发表于 2022年8月6日 19:04:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/73259202.html
匿名

发表评论

匿名网友

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

确定