Seleniuim操作浏览器的核心原理

面集团的时候被问到一个问题Selenium的问题:

问:你研究过Selenium的源码吗,原理是什么? 答:研究过, Selenium就是通过JsonWireProtocol来进行命令的传输。

问:那是如何传输的? 答:就是把session json数据通过url进入某个互斥锁端口传过去。

问:那数据是如何识别和操作也页面对象的? 答:就是通过塞入浏览器的js来操作页面对象的。

问:那是通过什么方式操作的?答:。。。

为了这个问题,我再翻了一遍Selenium的源码,又冒出很多新问题:

到了CommandExecutor发送了命令后,后面浏览器是怎么处理json的?

geckodriver.exe 这个东西是支持到最新的Firefox的,但和之前FirefoxDriver什么关系?

Marionette是什么东西? Firefox Puppeteer又是什么东西?

首先明确的一点是我这里看的都是针对Firefox的操作,其他的可能有别的实现方式

首先JsonWireProctol 已经过时了,取代它的是 W3C WebDriver standard   这个协议定义了所有对浏览器操作的协议。所有语言都必须以协议特定的格式通过HTTP来发送和接受命令。定义这个协议的好处就是不管你客户端用的什么语言发送的什么样的命令,到了不同的浏览器(IE,Firefox,Chrome,Opera)都会执行同样一种操作。

客户端发送的格式如下: /session/{session id }/url with body {"url": "http://example.com/"}.

到了服务端,也就是浏览器后,会有2种方式,一种是add-on,一种是塞入代码到浏览器中来接受这些命令,执行命令,返回response

那么现在说下Firefox服务端是如何实现对浏览器的控制的:

Marionette介绍: https://firefox-source-docs.mozilla.org/testing/marionette/marionette/index.html#marionette

(Marionette is an automation driver for Mozilla’s Gecko engine. It can remotely control either the UI or the internal JavaScript of Gecko-based browsers, such as Firefox and Fennec. It can control both the chrome and the content document, giving a high level of control and ability to replicate user interaction. In addition to performing actions on the browser, Marionette can also ready properties and attributes of the DOM.)

Marionette服务建立在Gecko引擎里面的一个python类库,但是它用的是一个不一样的协议,为了使得Marionette和WebDriver兼容,就会用到geckodriver, Selenium客户端告诉geckodriver,然后geckodriver告诉Marionette服务,由Marionette来操作浏览器。 不一样的协议导致Marionette接收到的客户端命令格式是这样的: [type, message ID, command, parameters] 

Mationette源码: https://searchfox.org/mozilla-central/source/testing/marionette

原文地址:https://www.cnblogs.com/goldenRazor/p/8409726.html