WebDriver protocol
In the early days Selenium
, they WebDriver
were two independent projects. In the Selenium
early days, they mainly relied on converting operations into JavaScript and then injecting them into the browser to automate WebDriver
the browser, encapsulating the browser's native API into a set of more object-oriented APIs, but due to the differences in the cores of different browsers, Therefore, in order to adapt, different APIs must be implemented for different browsers. In the Selenium2
following two items can be combined, we often say Selenium
it is mainly refers to the WebDriver
API. Selenium
The main version of the current stable version is 3, and the beta version is Selenium4
already on the way.
Selenium
Or WebDriver
the basis of interaction with the browser is the WebDriver
protocol (W3C recommended standard). WebDriver
The protocol is a RESTful Web service that uses JSON as the transmission format through the HTTP protocol and has nothing to do with the operating system and programming language.
WebDriver
There are currently two versions of the agreement:
WebDriver 1
:https://www.w3.org/TR/webdriver1/WebDriver 2
:https://www.w3.org/TR/webdriver/
WebDriver
There is an abandoned agreement before the JsonWireProtocol
agreement, seehttps://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol
WebDriver
Three objects are involved in the agreement model:
- Client (Client or local end): It can be simply regarded as the program or machine that calls the WebDriver API. Our commonly used Python WebDriver API is a Python language binding of the client.
- Server (Server or remote end): the running
RemoteWebDriver
machine, orWebDriver
the browser that supports the protocol , such as the supportedgeckodriver
Firefox browser and the supportedchromedriver
Chrome browser.WebDriver
The protocol actually only defines the behavior of the server. WebDriver
: The various browsersWebDriver
protocol, such asgeckodriver
,chromedriver
,WebDriver
is equivalent to the client and server intermediary, providingWebDriver
protocol RESTful services. The clientWebDriver
controls the server (browser). For example, Firefox relies onwebdriver.xpi
extensions to control the browser.
After the client runs, a session is started. In the session, the WebDriver
client requests the server through the protocol RESTful service endpoint (endpoint, which can be simply understood as url), and the server returns a response to the client after accepting the request.
!
WebDriver
The protocol maps endpoints to commands. The relationship between endpoints and commands is as follows ( https://www.w3.org/TR/webdriver/#endpoints ):
Method | URI Template | Command |
---|---|---|
POST | /session | New Session |
DELETE | /session/{session id} | Delete Session |
GET | /status | Status |
GET | /session/{session id}/timeouts | Get Timeouts |
POST | /session/{session id}/timeouts | Set Timeouts |
POST | /session/{session id}/url | Navigate To |
GET | /session/{session id}/url | Get Current URL |
POST | /session/{session id}/back | Back |
POST | /session/{session id}/forward | Forward |
POST | /session/{session id}/refresh | Refresh |
GET | /session/{session id}/title | Get Title |
GET | /session/{session id}/window | Get Window Handle |
DELETE | /session/{session id}/window | Close Window |
POST | /session/{session id}/window | Switch To Window |
GET | /session/{session id}/window/handles | Get Window Handles |
POST | /session/{session id}/window/new | New Window |
POST | /session/{session id}/frame | Switch To Frame |
POST | /session/{session id}/frame/parent | Switch To Parent Frame |
GET | /session/{session id}/window/rect | Get Window Rect |
POST | /session/{session id}/window/rect | Set Window Rect |
POST | /session/{session id}/window/maximize | Maximize Window |
POST | /session/{session id}/window/minimize | Minimize Window |
POST | /session/{session id}/window/fullscreen | Fullscreen Window |
GET | /session/{session id}/element/active | Get Active Element |
POST | /session/{session id}/element | Find Element |
POST | /session/{session id}/elements | Find Elements |
POST | /session/{session id}/element/{element id}/element | Find Element From Element |
POST | /session/{session id}/element/{element id}/elements | Find Elements From Element |
GET | /session/{session id}/element/{element id}/selected | Is Element Selected |
GET | /session/{session id}/element/{element id}/attribute/{name} | Get Element Attribute |
GET | /session/{session id}/element/{element id}/property/{name} | Get Element Property |
GET | /session/{session id}/element/{element id}/css/{property name} | Get Element CSS Value |
GET | /session/{session id}/element/{element id}/text | Get Element Text |
GET | /session/{session id}/element/{element id}/name | Get Element Tag Name |
GET | /session/{session id}/element/{element id}/rect | Get Element Rect |
GET | /session/{session id}/element/{element id}/enabled | Is Element Enabled |
GET | /session/{session id}/element/{element id}/computedrole | Get Computed Role |
GET | /session/{session id}/element/{element id}/computedlabel | Get Computed Label |
POST | /session/{session id}/element/{element id}/click | Element Click |
POST | /session/{session id}/element/{element id}/clear | Element Clear |
POST | /session/{session id}/element/{element id}/value | Element Send Keys |
GET | /session/{session id}/source | Get Page Source |
POST | /session/{session id}/execute/sync | Execute Script |
POST | /session/{session id}/execute/async | Execute Async Script |
GET | /session/{session id}/cookie | Get All Cookies |
GET | /session/{session id}/cookie/{name} | Get Named Cookie |
POST | /session/{session id}/cookie | Add Cookie |
DELETE | /session/{session id}/cookie/{name} | Delete Cookie |
DELETE | /session/{session id}/cookie | Delete All Cookies |
POST | /session/{session id}/actions | Perform Actions |
DELETE | /session/{session id}/actions | Release Actions |
POST | /session/{session id}/alert/dismiss | Dismiss Alert |
POST | /session/{session id}/alert/accept | Accept Alert |
GET | /session/{session id}/alert/text | Get Alert Text |
POST | /session/{session id}/alert/text | Send Alert Text |
GET | /session/{session id}/screenshot | Take Screenshot |
GET | /session/{session id}/element/{element id}/screenshot | Take Element Screenshot |
POST | /session/{session id}/print | Print Page |
WebDriver Python API执行流程(反推)
WebDriver
客户端API其实就是实现属性或方法到命令,再到WebDriver
协议端点的变换过程。
For example: WebDriver
the current_url
characteristics of the class
-
current_url
The characteristic is actuallyCommand.GET_CURRENT_URL
the result of the execution of the command .@property def current_url(self): return self.execute(Command.GET_CURRENT_URL)['value']
-
WebDriver
Theexecute
method of the class iscommand_executor.execute(driver_command, params)
executed by the command.
command_executor
Is a string (server url) orremote_connection.RemoteConnection
object. -
remote_connection.RemoteConnection
The class is locatedselenium\webdriver\remote\remote_connection.py
.
remote_connection.RemoteConnection
The_commands
attributes of the class define the relationship between the command and the endpoint.
Command.GET_CURRENT_URL: ('GET', '/session/$sessionId/url')
remote_connection.RemoteConnection
Theexecute
method of the class controls the server to execute commands. -
WebDriver
When the class is instantiatedcommand_executor
,command_executor
parameters are passed in , and the parameters are the server address.
WebDriver Python API execution process (in progress)
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
# 服务端地址
url = driver.command_executor._url
print(url)
The execution flow of the above code is roughly as follows:
-
driver = webdriver.Firefox()
That isselenium\webdriver\firefox\webdriver.py
, theWebDriver
class in the pair is instantiated, and theWebDriver
class inherits fromselenium\webdriver\remote\webdriver.py
theWebDriver
class in.WebDriver
Thecommand_executor
parameter in the class construction method is the server address.
At this point,WebDriver
start the browser, load thewebdriver.xpi
extension, and listen forWebDriver
protocol-based requests.
Ageckodriver.log
similar1116855012551 geckodriver INFO Listening on 127.0.0.1:54499
record can be seen in the log file . -
driver.get("http://www.baidu.com")
It is equivalent to executing aCommand.GET
command.WebDriver
Objects rely onremote_connection.RemoteConnection
objects tocommand_executor
execute commands. The mappingremote_connection.RemoteConnection
from command toWebDriver
protocol is implemented in the classCommand.GET: ('POST', '/session/$sessionId/url')
. Executing the command is equivalent to sending a corresponding request to the browser.WebDriver
Send the request to the browser.def get(self, url): self.execute(Command.GET, { 'url': url})
-
After accepting the request, the browser performs the operation (opens the Baidu page) and returns a response (server address).
WebDriver
Pass the response result to the code.