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

}
相关推荐
2501_941982053 分钟前
企微 RPA 自动化:打破数据孤岛,实现业务流程极致加速
自动化·企业微信·rpa
天空属于哈夫克311 分钟前
域群运营机器人:实现大规模社群标准化管理与自动化交互
机器人·自动化·交互
echome88822 分钟前
Go 语言并发编程:sync.WaitGroup 实战指南
开发语言·golang·xcode
天远Date Lab30 分钟前
天远入职背调报告API对接实战:Python构建自动化背景调查中台
大数据·网络·python·自动化
E_ICEBLUE1 小时前
Excel 删除空白行的 5 种方法 【手动管理与 Python 自动化】
python·自动化·excel
AAA小肥杨1 小时前
OpenClaw 和 GitHub 自动化,用于 PR 审核和 CI 监控
人工智能·ci/cd·自动化·大模型·github·openclaw
爆更小哇1 小时前
自动化测试避坑:iframe 嵌套页面定位元素失败问题全解析
selenium·自动化·测试用例
Night_Elf1 小时前
OpenClaw:完全零成本在Windows本机部署OpenClaw免费大模型指南
经验分享·自动化·运维开发·程序员创富
云飞云共享云桌面1 小时前
广东某智能装备工厂8人共享一台服务器
大数据·运维·服务器·人工智能·3d·自动化·电脑
鹓于1 小时前
OmniParser视觉鼠标自动化实战
运维·自动化·计算机外设