分享
golang模拟点击浏览器
YiYou.Org · · 20610 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
发现一个很好玩的浏览器模拟操作包,下面演示一下,打开网站,点击某个链接
package main
import (
"fmt"
"time"
"github.com/go-vgo/robotgo"
"sourcegraph.com/sourcegraph/go-selenium"
)
func main() {
var webDriver selenium.WebDriver
var err error
caps := selenium.Capabilities(map[string]interface{}{"browserName": "firefox"})
if webDriver, err = selenium.NewRemote(caps, "http://localhost:9515"); err != nil {
fmt.Printf("Failed to open session: %s\n", err)
return
}
defer webDriver.Quit()
err = webDriver.Get("http://www.yiyou.org")
if err != nil {
fmt.Printf("Failed to load page: %s\n", err)
return
}
robotgo.MoveMouseSmooth(323, 186) //移动鼠标
robotgo.MouseClick("left", true) //单击
time.Sleep(time.Second * 5)
robotgo.MoveMouseSmooth(422, 412)
robotgo.MouseClick("left", true)
time.Sleep(time.Second * 5)
cookies, err := webDriver.GetCookies() //获取cookie
if err != nil {
fmt.Println(err)
}
time.Sleep(time.Second * 5)
fmt.Print("Cookie:", cookies)
fmt.Print(robotgo.GetMousePos())
for {
}
}
需要先运行chromedriver,下载地址:
https://chromedriver.storage.proxy.ustclug.org/index.html
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信20610 次点击 ∙ 1 赞
上一篇:Golang面试题解析(三)
下一篇:golang反射机制介绍
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
发现一个很好玩的浏览器模拟操作包,下面演示一下,打开网站,点击某个链接
package main
import (
"fmt"
"time"
"github.com/go-vgo/robotgo"
"sourcegraph.com/sourcegraph/go-selenium"
)
func main() {
var webDriver selenium.WebDriver
var err error
caps := selenium.Capabilities(map[string]interface{}{"browserName": "firefox"})
if webDriver, err = selenium.NewRemote(caps, "http://localhost:9515"); err != nil {
fmt.Printf("Failed to open session: %s\n", err)
return
}
defer webDriver.Quit()
err = webDriver.Get("http://www.yiyou.org")
if err != nil {
fmt.Printf("Failed to load page: %s\n", err)
return
}
robotgo.MoveMouseSmooth(323, 186) //移动鼠标
robotgo.MouseClick("left", true) //单击
time.Sleep(time.Second * 5)
robotgo.MoveMouseSmooth(422, 412)
robotgo.MouseClick("left", true)
time.Sleep(time.Second * 5)
cookies, err := webDriver.GetCookies() //获取cookie
if err != nil {
fmt.Println(err)
}
time.Sleep(time.Second * 5)
fmt.Print("Cookie:", cookies)
fmt.Print(robotgo.GetMousePos())
for {
}
}
需要先运行chromedriver,下载地址:
https://chromedriver.storage.proxy.ustclug.org/index.html