背景

最近工作碰到了一些自动化的需求,要求可以自动化的上传文件。调研了 chromedp package - github.com/chromedp/chromedp - Go Packagesselenium package - github.com/tebeka/selenium - Go Packages 这两个库,最终根据场景选择了和 chrome 浏览器更适配的 chromedp。

使用

获取

1
go get -u github.com/chromedp/chromedp

顺便可以去 GitHub-chromedp 上找到源码看看官方样例~
目前官方给出的样例如下:

The following examples are currently available:

Example Description
click use a selector to click on an element
cookie set a HTTP cookie on requests
download_file do headless file downloads
download_image do headless image downloads
emulate emulate a specific device such as an iPhone
eval evaluate javascript and retrieve the result
fast extract and render data from a page
forecast extract and render data from a page
geoip extract and render data from a page
headers add extra HTTP headers to browser requests
keys send key events to an element
latlon retrieve the latitude/longitude from google maps, using the browser’s target events
logic more complex logic beyond simple actions
multi use headless-shell and a container (Docker, Podman, other)
pdf capture a pdf of a page
proxy authenticate a proxy server which requires authentication
remote connect to an existing Chrome DevTools instance using a remote WebSocket URL
screenshot take a screenshot of a specific element and of the entire browser viewport
submit fill out and submit a form
subtree populate and travel a subtree of the DOM
text 从选中的 element 中提取 text 文本
upload 以表单的形式上传一个文件
visible 等待某个 element 完全加载。