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

}
相关推荐
ん贤9 分钟前
自go-zero走进微服务
开发语言·后端·golang
AC赳赳老秦30 分钟前
Notion+DeepSeek:搭建个人工作看板与自动化任务管理规则
前端·javascript·人工智能·自动化·prometheus·notion·deepseek
weixin_4038101338 分钟前
EasyClick 安卓自动化版本 如何自激活代理模式并且启动安卓的自动化服务
android·自动化·代理模式
叫我:松哥44 分钟前
基于flask 智能体的教学演示文档生成及质量评价系统,集成了DeepSeek 大语言模型实现自动化文档生成和多维度质量评估
人工智能·机器学习·信息可视化·语言模型·数据分析·flask·自动化
weixin_462446233 小时前
一键安装 Hadoop 3.3.6 自动化脚本详解 |(含 JAVA_HOME 自动配置)
java·hadoop·自动化
运维行者_12 小时前
2026 技术升级,OpManager 新增 AI 网络拓扑与带宽预测功能
运维·网络·数据库·人工智能·安全·web安全·自动化
源代码•宸15 小时前
Leetcode—404. 左叶子之和【简单】
经验分享·后端·算法·leetcode·职场和发展·golang·dfs
AC赳赳老秦17 小时前
Confluence + DeepSeek:构建自动化、智能化的企业知识库文档生成与维护体系
大数据·运维·人工智能·自动化·jenkins·数据库架构·deepseek
Grassto18 小时前
10 Go 是如何下载第三方包的?GOPROXY 与源码解析
后端·golang·go·go module
源代码•宸19 小时前
Leetcode—513. 找树左下角的值【中等】
经验分享·算法·leetcode·面试·职场和发展·golang·dfs