On the realization of the principle of selenium python library mobilize webdriver drive browser

When used in a recent study web automation selenium library, I feel very magical, then pondering a moment, wrote a point of experience.

When we enter the following three lines of code and execution, you will find the newly opened up a browser window and access the Baidu home page, but this is how to do it?

1 from selenium import webdriver
2 driver = webdriver.Chrome()
3 driver.get('http://www.baidu.com')

 

First we look at the structure of selenium library:

Obviously, selenium is a software package, there are two first-level sub-packages, common and webdriver. After importing webdriver, webdriver.Chrome Chrome () is what is it?

Webdriver the original module in the two sub-packets from the class WebDriver under chrome, the driver = webdriver.Chrome driver () is a WebDriver instantiate an object class. Let's look at this class:

This class is doing it? It is the original control Google Chrome browser driver to drive, but a closer look, did not get to see it there way to die, oh, it inherits from RemoteWebDriver class, that is, two sub-package module under remote in webdriver the WebDriver like, Oh, this really is a high-frequency words ah! The method should get in there, to get to it:

Sure enough, get to call the execute method above, mass participation, we found that execute in turn calls the command_executor.execute method:

继续查看,发现command_executor.execute方法是remote_connection.py这个模块里面的RemoteConnection类下面的,

看这个类注释,连接到远程浏览器驱动服务,很显然,浏览器驱动是服务端,selenium是客户端。在下面找到execute方法:

给远程服务端发命令command,又将命令传给下面的_request方法,发送HTTP请求给远程服务端,即浏览器驱动,这里出现了大家熟悉的请求方法get或者post,请求url,请求体,再往上看command:

原来发的是post请求,这里使用的是WebDriver wire protocol协议,即JsonWireProtocol,body部分是这个协议规定的JSON格式的字符串。

总的来说,过程还是很复杂的,至少对于我来说。

补充:对于每一条Selenium脚本,一个http请求会被创建并且发送给浏览器的驱动,浏览器驱动中包含了一个HTTP Server,用来接收这些http请求,HTTP Server接收到请求后根据请求来具体操控对应的浏览器,浏览器执行具体的测试步骤,浏览器将步骤执行结果返回给HTTP Server,HTTP Server又将结果返回给Selenium的脚本,如果是错误的http代码我们就会在控制台看到对应的报错信息。

Guess you like

Origin www.cnblogs.com/wangyi0419/p/11353606.html