golang+selenium自动化+chrome浏览器操作

1,selenium 是自动化测试以及自动化爬取的框架,常用于python开发,今天这里使用golang selenium进行自动化开发,相比python,主要是社区广泛,golang同样也是跟python功能一样,同时go可以支持大并发携程 自动化爬取节省资源,下面我们就直接上代码案例操作

1,首先需要下载谷歌驱动,并配置到环境变量,必须与chrom版本相同

版本对比网站

c 复制代码
https://googlechromelabs.github.io/chrome-for-testing/

2,安装golang idea ,并配置golang环境变量

3,如果成功打开了谷歌浏览器,那么golang的selenium自动化配置完成,里面的代码自行配置了

核心代码

c 复制代码
func LeisuSelenumTwo() {
	opts := []selenium.ServiceOption{
		selenium.Output(os.Stderr), // Output debug information to STDERR.
	}
	selenium.SetDebug(true)
	service, err := selenium.NewChromeDriverService(seleniumPath, port, opts...)
	if err != nil {
		panic(err) // panic is used only as an example and is not otherwise recommended.
	}
	defer service.Stop()
	fmt.Println(">>>>>>>执行到这里了》》》》》》》》")
	// Connect to the WebDriver instance running locally.
	caps := selenium.Capabilities{"browserName": "chrome"}
	wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
	fmt.Println(">>>>>>>执行到这里222222222222222")

	if err != nil {
		panic(err)
	}
	fmt.Println("开始执行自动化")

	defer wd.Quit()
	fmt.Println(">>>>>>>>>>>>>")
	// Navigate to the simple playground interface.
	if err := wd.Get("http://www.baidu.com"); err != nil {
		panic(err)
	}

	time.Sleep(time.Second * 10)
	fmt.Println("自动化执行结束")

	// Get a reference to the text box containing code.
	//elem, err := wd.FindElement(selenium.ByCSSSelector, "#code")
	//if err != nil {
	//	panic(err)
	//}
	 Remove the boilerplate code already in the text box.
	//if err := elem.Clear(); err != nil {
	//	panic(err)
	//}
	//
	 Enter some new code in text box.
	//err = elem.SendKeys(`
	//    package main
	//    import "fmt"
	//    func main() {
	//        fmt.Println("Hello WebDriver!")
	//    }
	//`)
	//if err != nil {
	//	panic(err)
	//}
	//
	 Click the run button.
	//btn, err := wd.FindElement(selenium.ByCSSSelector, "#run")
	//if err != nil {
	//	panic(err)
	//}
	//if err := btn.Click(); err != nil {
	//	panic(err)
	//}
	//
	 Wait for the program to finish running and get the output.
	//outputDiv, err := wd.FindElement(selenium.ByCSSSelector, "#output")
	//if err != nil {
	//	panic(err)
	//}
	//
	//var output string
	//for {
	//	output, err = outputDiv.Text()
	//	if err != nil {
	//		panic(err)
	//	}
	//	if output != "Waiting for remote server..." {
	//		break
	//	}
	//	time.Sleep(time.Millisecond * 100)
	//}
	//
	//fmt.Printf("%s", strings.Replace(output, "\n\n", "\n", -1))

}
相关推荐
cxyll12347 小时前
postman+newman+jenkins接口自动化
自动化·jenkins·postman
Linux运维技术栈9 小时前
多系统 Node.js 环境自动化部署脚本:从 Ubuntu 到 CentOS,再到版本自由定制
linux·ubuntu·centos·node.js·自动化
xcs1940510 小时前
AI 自动化编程 trae 体验 页面添加富编辑器
运维·自动化·编辑器
bianshaopeng12 小时前
ubuntu go 环境变量配置
开发语言·ubuntu·golang
元清加油12 小时前
【Goland】:协程和通道
服务器·开发语言·后端·网络协议·golang
lpfasd12312 小时前
01_Go语言基础与环境搭建
开发语言·后端·golang
AI人工智能+14 小时前
一种融合AI与OCR的施工许可证识别技术,提升工程监管效率,实现自动化、精准化处理。
人工智能·自动化·ocr·施工许可证识别
专注API从业者21 小时前
Python + 淘宝 API 开发:自动化采集商品数据的完整流程
大数据·运维·前端·数据挖掘·自动化
中科米堆1 天前
中科米堆CASAIM自动化三维测量设备测量汽车壳体直径尺寸
运维·自动化·汽车·视觉检测
程序员三藏1 天前
Selenium+python自动化测试:解决无法启动IE浏览器及报错问题
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例