Golang使用selenium操作Chrome

时间:2020-05-29 10:34:36   收藏:0   阅读:46
Golang使用selenium操作Chrome

1.需求

? 解决自动化登录的问题,顺便可以解决爬虫问题。

2.基本概念

? selenium: Selenium 是一个用于 Web 应用程序测试的工具,Selenium 测试直接自动运行在浏览器中,就像真正的用户在手工操作一样。

? webdriver: chromeDriver是谷歌为网站开发人员提供的自动化测试工具。

? selenium和webdriver其实原来是两个不同的开源项目,后来selenium2就把selenium1(RC)和webdriver合并到一起,还是用selenium的名字,但是实现方式和协议基本沿用的是webdriver的。可以看做一样。

? 简单来说,需要通过chromedriver调用chrome,进行模拟浏览器操作。

3.安装

4.源码分析

4.1 selenium执行流程分析

? 经过我的理解和思考,我认为selenium 主要运行模式如下(个人理解,仅供参考)。

技术分享图片

? 程序需要调用selenium库执行相应的函数, 后台调用chrome浏览器,然后将操作元素的请求给下方的浏览器驱动。浏览器驱动再转发这个请求给浏览器。最后将结果返回。

4.2 源码文件分析selenium-golangdoc

? 因为selenium源码包不是很大,同时因为是chrome进行实战,所以我将源码进行删减然后进行添加注释。godoc包应该没有写完整,比如webdriver,webelement只讲了接口,并没有将实现细节,我们可以根据selenium操作进行脑补。可以参考python中selenium中的实现,比如自动化测试

? 先继续画个图进行包的讲解的吧。

技术分享图片

? 源码简略分析:

// 第一类 杂项

//删除会话
func DeleteSession(urlPrefix, id string) error

//开启关闭debug调试
func SetDebug(debug bool)

//设置代理
func (c Capabilities) AddProxy(p Proxy)
//设置日志级别
func (c Capabilities) SetLogLevel(typ log.Type, level log.Level)

// 第二类 seleium后台服务

//服务实例的可选项
type ServiceOption func(*Service) error

//添加chrome路径信息,返回的是serviceOption类型
func ChromeDriver(path string) ServiceOption

//服务的结构体,包含隐藏类型
type Service struct {
    // contains filtered or unexported fields
}

//启动chrome浏览器的服务器,返回service类型指针
func NewChromeDriverService(path string, port int, opts ...ServiceOption) (*Service, error)

//关闭服务,记得defer关闭
func (s *Service) Stop() error

// 第三类 chrome操作相关

//设置浏览器兼容性,map类型,比如chrome浏览器兼容性。
//caps := selenium.Capabilities{"browserName": "chrome"}
type Capabilities map[string]interface{}

//通过调用函数添加chrome兼容性
func (c Capabilities) AddChrome(f chrome.Capabilities)

//启动webdriver实例
func NewRemote(capabilities Capabilities, urlPrefix string) (WebDriver, error)

//通过WebDriver接口可以看出具体页面的实现的方法,是接口,接口里面是实现的方法。
type WebDriver interface {
    //返回服务器环境的版本信息
    // Status returns various pieces of information about the server environment.
    Status() (*Status, error)

    //创建新的session
    // NewSession starts a new session and returns the session ID.
    NewSession() (string, error)

    //创建新的session(已废弃)
    // SessionId returns the current session ID
    // Deprecated: This identifier is not Go-style correct. Use SessionID
    // instead.
    SessionId() string

    //获取新的ssionid
    // SessionID returns the current session ID.
    SessionID() string

    //切换session
    // SwitchSession switches to the given session ID.
    SwitchSession(sessionID string) error

    //返回兼容性
    // Capabilities returns the current session‘s capabilities.
    Capabilities() (Capabilities, error)

    //设置异步脚本执行时间
    // SetAsyncScriptTimeout sets the amount of time that asynchronous scripts
    // are permitted to run before they are aborted. The timeout will be rounded
    // to nearest millisecond.
    SetAsyncScriptTimeout(timeout time.Duration) error

    //设置等待搜索元素的时间,目的: 如果页面结果返回较慢,就需要等待页面内容完整返回,然后再进行页面元素操作。
    // SetImplicitWaitTimeout sets the amount of time the driver should wait when
    // searching for elements. The timeout will be rounded to nearest millisecond.
    SetImplicitWaitTimeout(timeout time.Duration) error

    //设置等待页面的时间
    // SetPageLoadTimeout sets the amount of time the driver should wait when
    // loading a page. The timeout will be rounded to nearest millisecond.
    SetPageLoadTimeout(timeout time.Duration) error

    //设置会话退出
    // Quit ends the current session. The browser instance will be closed.
    Quit() error

    //获取现在窗口句柄,一串序号,打开一个窗口一个句柄
    // CurrentWindowHandle returns the ID of current window handle.
    CurrentWindowHandle() (string, error)

    //获取现在所有打开窗口句柄,获取所有窗口句柄
    // WindowHandles returns the IDs of current open windows.
    WindowHandles() ([]string, error)

    //返回当前页面连接的URL
    // CurrentURL returns the browser‘s current URL.
    CurrentURL() (string, error)

    //获取当前页面的标题
    // Title returns the current page‘s title.
    Title() (string, error)

    //返回当前页面的所有内容
    // PageSource returns the current page‘s source.
    PageSource() (string, error)

    //关闭现在的窗口
    // Close closes the current window.
    Close() error

    //切换frame,frame里面内嵌一个完整html,如果操作里面的内容需要进入iframe中。switchframe(nil),返回到顶层
    // SwitchFrame switches to the given frame. The frame parameter can be the
    // frame‘s ID as a string, its WebElement instance as returned by
    // GetElement, or nil to switch to the current top-level browsing context.
    SwitchFrame(frame interface{}) error

    切换windows到指定窗口
    // SwitchWindow switches the context to the specified window.
    SwitchWindow(name string) error

    //关闭窗口
    // CloseWindow closes the specified window.
    CloseWindow(name string) error

    //设置最大化窗口
    // MaximizeWindow maximizes a window. If the name is empty, the current
    // window will be maximized.
    MaximizeWindow(name string) error

    //设置窗口尺寸
    // ResizeWindow changes the dimensions of a window. If the name is empty, the
    // current window will be maximized.
    ResizeWindow(name string, width, height int) error

    //通过url导航至相应界面。主要选项,就是打开url地址。
    // Get navigates the browser to the provided URL.
    Get(url string) error

    //向前翻
    // Forward moves forward in history.
    Forward() error

    //向后翻
    // Back moves backward in history.
    Back() error

    //刷新
    // Refresh refreshes the page.
    Refresh() error

    //查找定位一个html元素。
    // FindElement finds exactly one element in the current page‘s DOM.
    FindElement(by, value string) (WebElement, error)

    //查找定位多个的html元素
    // FindElement finds potentially many elements in the current page‘s DOM.
    FindElements(by, value string) ([]WebElement, error)

    //获取当前焦点元素
    // ActiveElement returns the currently active element on the page.
    ActiveElement() (WebElement, error)

    //解码元素响应
    // DecodeElement decodes a single element response.
    DecodeElement([]byte) (WebElement, error)

    //解码多个元素响应
    // DecodeElements decodes a multi-element response.
    DecodeElements([]byte) ([]WebElement, error)

    //获取所有cookie
    // GetCookies returns all of the cookies in the browser‘s jar.
    GetCookies() ([]Cookie, error)

    //获取指定cookie
    // GetCookie returns the named cookie in the jar, if present. This method is
    // only implemented for Firefox.
    GetCookie(name string) (Cookie, error)

    //添加cookie到jar
    // AddCookie adds a cookie to the browser‘s jar.
    AddCookie(cookie *Cookie) error

    //删除所有cookie
    // DeleteAllCookies deletes all of the cookies in the browser‘s jar.    
    DeleteAllCookies() error

    //删除指定cookie
    // DeleteCookie deletes a cookie to the browser‘s jar.
    DeleteCookie(name string) error

    //敲击鼠标按钮
    // Click clicks a mouse button. The button should be one of RightButton,
    // MiddleButton or LeftButton.
    Click(button int) error

    //双击鼠标按钮
    // DoubleClick clicks the left mouse button twice.
    DoubleClick() error

    //按下鼠标
    // ButtonDown causes the left mouse button to be held down.
    ButtonDown() error

    //抬起鼠标
    // ButtonUp causes the left mouse button to be released.
    ButtonUp() error

    //发送更改到活动元素(已丢弃)
    // SendModifier sends the modifier key to the active element. The modifier
    // can be one of ShiftKey, ControlKey, AltKey, MetaKey.
    //
    // Deprecated: Use KeyDown or KeyUp instead.
    SendModifier(modifier string, isDown bool) error

    //将按键顺序序列发送到活动元素
    // KeyDown sends a sequence of keystrokes to the active element. This method
    // is similar to SendKeys but without the implicit termination. Modifiers are
    // not released at the end of each call.
    KeyDown(keys string) error

    //释放发送的元素
    // KeyUp indicates that a previous keystroke sent by KeyDown should be
    // release
    KeyUp(keys string) error

    //拍摄快照
    // Screenshot takes a screenshot of the browser window.
    Screenshot() ([]byte, error)

    //日志抓取
    // Log fetches the logs. Log types must be previously configured in the
    // capabilities.
    //
    // NOTE: will return an error (not implemented) on IE11 or Edge drivers.
    Log(typ log.Type) ([]log.Message, error)

    //解除警报
    // DismissAlert dismisses current alert.
    DismissAlert() error

    //接受警报
    // AcceptAlert accepts the current alert.
    AcceptAlert() error

    //返回现在警报内容
    // AlertText returns the current alert text.
    AlertText() (string, error)

    //发送警报内容
    // SetAlertText sets the current alert text.
    SetAlertText(text string) error

    //执行脚本
    // ExecuteScript executes a script.
    ExecuteScript(script string, args []interface{}) (interface{}, error)

    //异步执行脚本
    // ExecuteScriptAsync asynchronously executes a script.
    ExecuteScriptAsync(script string, args []interface{}) (interface{}, error)

    //执行源脚本
    // ExecuteScriptRaw executes a script but does not perform JSON decoding.
    ExecuteScriptRaw(script string, args []interface{}) ([]byte, error)

    //异步执行源脚本
    // ExecuteScriptAsyncRaw asynchronously executes a script but does not
    // perform JSON decoding.
    ExecuteScriptAsyncRaw(script string, args []interface{}) ([]byte, error)

    //等待条件为真
    // WaitWithTimeoutAndInterval waits for the condition to evaluate to true.
    WaitWithTimeoutAndInterval(condition Condition, timeout, interval time.Duration) error

    //等待时间
    // WaitWithTimeout works like WaitWithTimeoutAndInterval, but with default polling interval.
    WaitWithTimeout(condition Condition, timeout time.Duration) error

    //等待
    //Wait works like WaitWithTimeoutAndInterval, but using the default timeout and polling interval.
    Wait(condition Condition) error
}

//对相关元素进行后续执行,接口类型,里面是实现方法
type WebElement interface {
    // click选中的元素
    // Click clicks on the element.
    Click() error

    //发送数据到选中元素
    // SendKeys types into the element.
    SendKeys(keys string) error

    //提交按钮
    // Submit submits the button.
    Submit() error

    //清空按钮
    // Clear clears the element.
    Clear() error

    //移动元素到相应的坐标
    // MoveTo moves the mouse to relative coordinates from center of element, If
    // the element is not visible, it will be scrolled into view.
    MoveTo(xOffset, yOffset int) error

    // 查找子元素
    // FindElement finds a child element.
    FindElement(by, value string) (WebElement, error)

    //查找多个子元素
    // FindElement finds multiple children elements.
    FindElements(by, value string) ([]WebElement, error)

    //返回标签名称
    // TagName returns the element‘s name.
    TagName() (string, error)

    //返回元素内容
    // Text returns the text of the element.
    Text() (string, error)

    //元素被选中返回真
    // IsSelected returns true if element is selected.
    IsSelected() (bool, error)

    //如果元素启用返回真
    // IsEnabled returns true if the element is enabled.
    IsEnabled() (bool, error)

    //如果元素显示返回真
    // IsDisplayed returns true if the element is displayed.
    IsDisplayed() (bool, error)
    //获取元素的名称
    // GetAttribute returns the named attribute of the element.
    GetAttribute(name string) (string, error)

    //范围元素的位置
    // Location returns the element‘s location.
    Location() (*Point, error)

    //滚动后返回元素的位置
    // LocationInView returns the element‘s location once it has been scrolled
    // into view.
    LocationInView() (*Point, error)

    //返回元素的大小
    // Size returns the element‘s size.
    Size() (*Size, error)

    //返回css优先级
    // CSSProperty returns the value of the specified CSS property of the
    // element.
    CSSProperty(name string) (string, error)

    //返回属性滚动的快照
    // Screenshot takes a screenshot of the attribute scroll‘ing if necessary.
    Screenshot(scroll bool) ([]byte, error)
}
4.3 说明

5.基础操作

? 操作之前,先分享一个快速获取CSS Selector和xpath的方法。

? 大家用chrome浏览器访问网页,按F12后,点击调试页左上角Elements箭头,然后鼠标移动到目的位置,即可显示页面对应的HTML 元素。

? 右键选中的元素,选择copy,此时可以根据直接选择ID,CLASS,CSS Selector或Xpath的地址.

技术分享图片

参考别人的案例,写了几个小案例,仅供参考。亮代码吧。

结束语

? 个人理解,仅供参考。如有错误,欢迎指正。

? 一直在路上,默默前行。

?

参考

selenium的golang代码包

selenium-golangdoc

自动化测试

golang driver使用记录

windows对象

原文:https://blog.51cto.com/9406836/2499426

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!