近期,会统一阅读 selenium 源码以及 pytest 的源码。在阅读过程中,重在理解 selenium 和 pytest 的工作原理,同时对薄弱部分的基础知识进行补充,在此进行记录。

python 基础知识

Selenium 源码阅读/使用

这段代码中,command_info[0] 定义了 method(POST,GET...), url 为 RemotewebDriver 的 url,body 是需要传输给 webdriver 的数据,内容在上面被格式化了成 json 格式。所以通俗来讲就是,通过_request() 方法,将操作 (data) 发送给 webdriver 去执行并获取返回值。如果不继续看底层代码的话,这里就结束了 selenium 的工作原理。

其实,这里只看了一个建立 session 的过程,但是大部分的操作(比方说打开浏览器,get(url), find_element())都是使用的相同的方式,只不过是使用了维护的字典中不一样的信息,如下:

self._commands = {
            Command.NEW_SESSION: ('POST', '/session'),
            Command.QUIT: ('DELETE', '/session/$sessionId'),
            Command.W3C_GET_CURRENT_WINDOW_HANDLE:
                ('GET', '/session/$sessionId/window'),
            Command.W3C_GET_WINDOW_HANDLES:
                ('GET', '/session/$sessionId/window/handles'),
            Command.GET: ('POST', '/session/$sessionId/url'),
            Command.GO_FORWARD: ('POST', '/session/$sessionId/forward'),
            Command.GO_BACK: ('POST', '/session/$sessionId/back'),
            Command.REFRESH: ('POST', '/session/$sessionId/refresh'),
            Command.W3C_EXECUTE_SCRIPT:
                ('POST', '/session/$sessionId/execute/sync'),
            Command.W3C_EXECUTE_SCRIPT_ASYNC:
                ('POST', '/session/$sessionId/execute/async'),
            Command.GET_CURRENT_URL: ('GET', '/session/$sessionId/url'),
            Command.GET_TITLE: ('GET', '/session/$sessionId/title'),
            Command.GET_PAGE_SOURCE: ('GET', '/session/$sessionId/source'),
            Command.SCREENSHOT: ('GET', '/session/$sessionId/screenshot'),
            Command.ELEMENT_SCREENSHOT: ('GET', '/session/$sessionId/element/$id/screenshot'),
            Command.FIND_ELEMENT: ('POST', '/session/$sessionId/element'),
            Command.FIND_ELEMENTS: ('POST', '/session/$sessionId/elements'),
            Command.W3C_GET_ACTIVE_ELEMENT: ('GET', '/session/$sessionId/element/active'),
            Command.FIND_CHILD_ELEMENT:
                ('POST', '/session/$sessionId/element/$id/element'),
            Command.FIND_CHILD_ELEMENTS:
                ('POST', '/session/$sessionId/element/$id/elements'),
            Command.CLICK_ELEMENT: ('POST', '/session/$sessionId/element/$id/click'),
            Command.CLEAR_ELEMENT: ('POST', '/session/$sessionId/element/$id/clear'),
            Command.GET_ELEMENT_TEXT: ('GET', '/session/$sessionId/element/$id/text'),
            Command.SEND_KEYS_TO_ELEMENT:
                ('POST', '/session/$sessionId/element/$id/value'),
            Command.UPLOAD_FILE: ('POST', "/session/$sessionId/se/file"),
            Command.GET_ELEMENT_TAG_NAME:
                ('GET', '/session/$sessionId/element/$id/name'),
            Command.IS_ELEMENT_SELECTED:
                ('GET', '/session/$sessionId/element/$id/selected'),
            Command.IS_ELEMENT_ENABLED:
                ('GET', '/session/$sessionId/element/$id/enabled'),
            Command.GET_ELEMENT_RECT:
                ('GET', '/session/$sessionId/element/$id/rect'),
            Command.GET_ELEMENT_ATTRIBUTE:
                ('GET', '/session/$sessionId/element/$id/attribute/$name'),
            Command.GET_ELEMENT_PROPERTY:
                ('GET', '/session/$sessionId/element/$id/property/$name'),
            Command.GET_ELEMENT_ARIA_ROLE:
                ('GET', '/session/$sessionId/element/$id/computedrole'),
            Command.GET_ELEMENT_ARIA_LABEL:
                ('GET', '/session/$sessionId/element/$id/computedlabel'),
            Command.GET_SHADOW_ROOT:
                ('GET', '/session/$sessionId/element/$id/shadow'),
            Command.FIND_ELEMENT_FROM_SHADOW_ROOT:
                ('POST', '/session/$sessionId/shadow/$shadowId/element'),
            Command.FIND_ELEMENTS_FROM_SHADOW_ROOT:
                ('POST', '/session/$sessionId/shadow/$shadowId/elements'),
            Command.GET_ALL_COOKIES: ('GET', '/session/$sessionId/cookie'),
            Command.ADD_COOKIE: ('POST', '/session/$sessionId/cookie'),
            Command.GET_COOKIE: ('GET', '/session/$sessionId/cookie/$name'),
            Command.DELETE_ALL_COOKIES:
                ('DELETE', '/session/$sessionId/cookie'),
            Command.DELETE_COOKIE:
                ('DELETE', '/session/$sessionId/cookie/$name'),
            Command.SWITCH_TO_FRAME: ('POST', '/session/$sessionId/frame'),
            Command.SWITCH_TO_PARENT_FRAME: ('POST', '/session/$sessionId/frame/parent'),
            Command.SWITCH_TO_WINDOW: ('POST', '/session/$sessionId/window'),
            Command.NEW_WINDOW: ('POST', '/session/$sessionId/window/new'),
            Command.CLOSE: ('DELETE', '/session/$sessionId/window'),
            Command.GET_ELEMENT_VALUE_OF_CSS_PROPERTY:
                ('GET', '/session/$sessionId/element/$id/css/$propertyName'),
            Command.EXECUTE_ASYNC_SCRIPT: ('POST', '/session/$sessionId/execute_async'),
            Command.SET_TIMEOUTS:
                ('POST', '/session/$sessionId/timeouts'),
            Command.GET_TIMEOUTS:
                ('GET', '/session/$sessionId/timeouts'),
            Command.W3C_DISMISS_ALERT:
                ('POST', '/session/$sessionId/alert/dismiss'),
            Command.W3C_ACCEPT_ALERT:
                ('POST', '/session/$sessionId/alert/accept'),
            Command.W3C_SET_ALERT_VALUE:
                ('POST', '/session/$sessionId/alert/text'),
            Command.W3C_GET_ALERT_TEXT:
                ('GET', '/session/$sessionId/alert/text'),
            Command.W3C_ACTIONS:
                ('POST', '/session/$sessionId/actions'),
            Command.W3C_CLEAR_ACTIONS:
                ('DELETE', '/session/$sessionId/actions'),
            Command.SET_WINDOW_RECT:
                ('POST', '/session/$sessionId/window/rect'),
            Command.GET_WINDOW_RECT:
                ('GET', '/session/$sessionId/window/rect'),
            Command.W3C_MAXIMIZE_WINDOW:
                ('POST', '/session/$sessionId/window/maximize'),
            Command.SET_SCREEN_ORIENTATION:
                ('POST', '/session/$sessionId/orientation'),
            Command.GET_SCREEN_ORIENTATION:
                ('GET', '/session/$sessionId/orientation'),
            Command.GET_NETWORK_CONNECTION:
                ('GET', '/session/$sessionId/network_connection'),
            Command.SET_NETWORK_CONNECTION:
                ('POST', '/session/$sessionId/network_connection'),
            Command.GET_LOG:
                ('POST', '/session/$sessionId/se/log'),
            Command.GET_AVAILABLE_LOG_TYPES:
                ('GET', '/session/$sessionId/se/log/types'),
            Command.CURRENT_CONTEXT_HANDLE:
                ('GET', '/session/$sessionId/context'),
            Command.CONTEXT_HANDLES:
                ('GET', '/session/$sessionId/contexts'),
            Command.SWITCH_TO_CONTEXT:
                ('POST', '/session/$sessionId/context'),
            Command.FULLSCREEN_WINDOW:
                ('POST', '/session/$sessionId/window/fullscreen'),
            Command.MINIMIZE_WINDOW:
                ('POST', '/session/$sessionId/window/minimize'),
            Command.PRINT_PAGE:
                ('POST', '/session/$sessionId/print'),
            Command.ADD_VIRTUAL_AUTHENTICATOR:
                ('POST', '/session/$sessionId/webauthn/authenticator'),
            Command.REMOVE_VIRTUAL_AUTHENTICATOR:
                ('DELETE', '/session/$sessionId/webauthn/authenticator/$authenticatorId'),
            Command.ADD_CREDENTIAL:
                ('POST', '/session/$sessionId/webauthn/authenticator/$authenticatorId/credential'),
            Command.GET_CREDENTIALS:
                ('GET', '/session/$sessionId/webauthn/authenticator/$authenticatorId/credentials'),
            Command.REMOVE_CREDENTIAL:
                ('DELETE', '/session/$sessionId/webauthn/authenticator/$authenticatorId/credentials/$credentialId'),
            Command.REMOVE_ALL_CREDENTIALS:
                ('DELETE', '/session/$sessionId/webauthn/authenticator/$authenticatorId/credentials'),
            Command.SET_USER_VERIFIED:
                ('POST', '/session/$sessionId/webauthn/authenticator/$authenticatorId/uv'),
        }

接下来还会继续去看源码,找一找有没有其他的用得是不一样的方式来触发一条请求的发送。


↙↙↙阅读原文可查看相关链接,并与作者交流