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

}
相关推荐
Lossya16 分钟前
【自动化测试】UI自动化的分类、如何选择合适的自动化测试工具以及其中appium的设计理念、引擎和引擎如何工作
自动化测试·测试工具·ui·appium·自动化
企业管理8MSaaS1 小时前
了解CRM销售自动化:类型、优势、策略和工具
运维·自动化
白总Server1 小时前
MongoDB解说
开发语言·数据库·后端·mongodb·golang·rust·php
laofashi20151 小时前
AirTest 基本操作范例和参数解释(一)
python·自动化·automation
创小董2 小时前
智能机巢+无人机:自动化巡检技术详解
运维·自动化·无人机
henanxiaoman2 小时前
SaltStack自动化运维部署
运维·自动化·saltstack
翔云API2 小时前
身份证识别接口的应用场景和作用
运维·服务器·开发语言·自动化·ocr
测试界柠檬4 小时前
面试真题 | web自动化关闭浏览器,quit()和close()的区别
前端·自动化测试·软件测试·功能测试·程序人生·面试·自动化
liupenglove5 小时前
golang操作mysql利器-gorm
mysql·golang
Rookie_explorers7 小时前
Linux下go环境安装、环境配置并执行第一个go程序
linux·运维·golang