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))

}
相关推荐
GAOJ_K1 小时前
滚柱导轨安装后如精度偏差对机械设备有影响吗?
运维·人工智能·科技·自动化·制造
爱学习的小囧2 小时前
零门槛!VCF 自动化环境登录 vSphere Supervisor 全教程
运维·服务器·算法·自动化·vmware·虚拟化
007张三丰2 小时前
软件测试专栏(7/20):接口测试全攻略:Postman+Newman实现API自动化
自动化·lua·接口测试·postman·api测试·newman
沃和莱特2 小时前
Copy as fetch + Skill:自动化问题记录分析的实践与思考
运维·ai·自动化·编程·skills
赛博云推-Twitter热门霸屏工具2 小时前
从手动运营到自动化增长:赛博云推让Twitter推广效率提升10倍
运维·自动化·twitter
Crazy CodeCrafter2 小时前
租金要交,但客流为零,要关店了?
大数据·运维·经验分享·自动化·开源软件
北京耐用通信3 小时前
耐达讯自动化CC linkie转Devicenet网关:架起三菱PLC与电导率仪跨协议“沟通之桥”
人工智能·物联网·网络协议·自动化·信息与通信
不会写DN4 小时前
Golang中实时推送的功臣 - WebSocket
开发语言·后端·golang
weixin_403810134 小时前
EasyClick iOS USB版本蓝牙点击坐标代码
ios·自动化·代理模式
`Jay5 小时前
高并发数据采集:隧道代理池架构设计与实现
爬虫·python·学习·golang·代理模式