测试工具 selenium底层原理详解 - command_executor) is bytes or isinstance(self

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力平台 👁️ 325 次阅读

目录

1、selenium版本的演变

1.1、Selenium 1.x(Selenium RC时代)

1.2、Selenium 2.x(WebDriver整合时代)

1.3、Selenium 3.x +

2、selenium原理说明

3、源码说明

3.1、启动webdriver服务建立连接

3.2、发送操作

1、selenium版本的演变

1.1、Selenium 1.x(Selenium RC时代)

核心原理:

Selenium RC(Remote Control):Selenium 1.x主要通过Selenium RC来实现自动化测试。Selenium RC启动一个Server,该Server负责控制浏览器行为。JavaScript注入技术:Selenium RC将操作Web元素的API调用转化为JavaScript代码,然后通过Selenium Core(一堆JavaScript函数的集合)注入到浏览器中执行。这种方式依赖于浏览器对JavaScript的支持,但速度较慢且稳定性依赖于Selenium内核对API的JavaScript翻译质量。

1.2、Selenium 2.x(WebDriver整合时代)

核心原理:

WebDriver的引入:Selenium 2.x整合了WebDriver项目,使得Selenium更加强大。WebDriver利用浏览器原生的API,封装成一套面向对象的Selenium WebDriver API,直接操作浏览器页面里的元素,甚至操作浏览器本身(如截屏、窗口大小调整、启动/关闭浏览器等)。浏览器原生API:由于使用浏览器原生API,WebDriver的速度大大提升,且调用的稳定性交给了浏览器厂商本身。然而,不同浏览器厂商对Web元素的操作和呈现存在差异,因此WebDriver需要为不同浏览器提供不同的实现(如ChromeDriver、FirefoxDriver等)。WebDriver Wire协议:WebDriver启动后会在特定端口上启动基于WebDriver Wire协议的Web Service,所有对WebDriver的API调用都会通过HTTP请求发送给这个Web Service。

1.3、Selenium 3.x +

核心原理:

继承2.x的特性:Selenium 3.x在底层原理上与Selenium 2.x保持一致,继续利用WebDriver和浏览器原生API进行操作。新增特性:Selenium 3.x加入了对更多浏览器原生驱动的支持,如Edge和Safari的原生驱动,以及更新了对Firefox的支持(通过geckodriver)。移除Selenium RC:与Selenium 2.x相比,Selenium 3.x去除了Selenium RC组件,更加专注于WebDriver的使用。

新增功能和API:为了满足用户不断变化的需求,Selenium会引入新的功能和API,以支持更复杂的测试场景和用例。

2、selenium原理说明

说明:这里说的原理都是整合了WebDriver之后的selenium版本。

思考:selenium是如何驱动浏览器做各种操作的呢?

分析:

首先我们想想,我们可以直接和浏览器交互吗,显然是不能,这时候就需要借助一个代理人帮我们做这件事,这个代理人就是WebDriver,我们不知道浏览器内核的各种API,难道浏览器厂商还不知道吗,所以他们就提供这样一个代理人给我们使用。也就是我们现在知道WebDriver提供一个服务,我们去请求这个服务把对浏览器的操作通过HTTP请求发送给WebDriver这个服务,再由它把操作解析后去调用浏览器的API,最终结果原路返回。这个时候我们还需要把这些操作统一起来才行,不然不太可能我们自己总是去调用接口发送请求吧,这时候selenium client就出现了,它在内部帮我们处理好了底层通信的一切,还把对浏览器的操作统一封装成一个个函数供给我们操作,我们只需要关心操作和操作返回的结果就行。综上就是整个selenium做的事情了。

把上面的过程在提炼一下,流程如下:

1.对于每一条Selenium脚本,一个http请求会被创建并且发送给浏览器的驱动,最开始建立连接时服务端返回一个sessionid给客户端,后续的交互都是通过sessionid进行交互2.浏览器驱动中包含了一个HTTP Server,用来接收这些http请求3.HTTP Server接收到请求后根据请求来具体操控对应的浏览器4.浏览器执行具体的测试步骤5.浏览器将步骤执行结果返回给HTTP Server6.HTTP Server又将结果返回给Selenium的脚本,如果是错误的http代码我们就会在控制台看到对应的报错信息。

3、源码说明

说明:我们从源码的角度看看,底层是如何进行交互的

3.1、启动webdriver服务建立连接

代码如下:

from selenium import webdriver

driver_path = 'E:\PycharmProjects\webUiTest\env\Scripts\chromedriver'

driver = webdriver.Chrome(executable_path=driver_path)

1、我们看看代码webdriver.Chrome(executable_path=driver_path)做了什么事情,按住ctrl键点击Chrome进入源码查看:

def __init__(self, executable_path="chromedriver", port=0,

options=None, service_args=None,

desired_capabilities=None, service_log_path=None,

chrome_options=None, keep_alive=True):

if chrome_options:

warnings.warn('use options instead of chrome_options',

DeprecationWarning, stacklevel=2)

options = chrome_options

if options is None:

# desired_capabilities stays as passed in

if desired_capabilities is None:

desired_capabilities = self.create_options().to_capabilities()

else:

if desired_capabilities is None:

desired_capabilities = options.to_capabilities()

else:

desired_capabilities.update(options.to_capabilities())

self.service = Service(

executable_path,

port=port,

service_args=service_args,

log_path=service_log_path)

self.service.start()

try:

RemoteWebDriver.__init__(

self,

command_executor=ChromeRemoteConnection(

remote_server_addr=self.service.service_url,

keep_alive=keep_alive),

desired_capabilities=desired_capabilities)

except Exception:

self.quit()

raise

self._is_remote = False

2、我们知道webdriver.Chrome()就是建立服务连接的过程,所以我们看到建立服务相关的代码就是:

我们在进入到self.service.start()源码看看它做了什么,源码如下:

def start(self):

"""

Starts the Service.

:Exceptions:

- WebDriverException : Raised either when it can't start the service

or when it can't connect to the service

"""

try:

cmd = [self.path]

cmd.extend(self.command_line_args())

self.process = subprocess.Popen(cmd, env=self.env,

close_fds=platform.system() != 'Windows',

stdout=self.log_file,

stderr=self.log_file,

stdin=PIPE)

except TypeError:

pass

3、原来是通过subprocess.Popen()函数根据我们传过来的chromedriver路径,开启一个子进程来执行打开chromedriver服务的命令。

4、但是别急,到这里只是把webdriver服务开启了,还没有初始化driver对象,继续回到源码,初始化driver对象肯定是在开启服务之后,也就是下面的源码:

5、我们继续进入看看它做了什么事情:

def __init__(self, command_executor='http://127.0.0.1:4444/wd/hub',

desired_capabilities=None, browser_profile=None, proxy=None,

keep_alive=False, file_detector=None, options=None):

"""

Create a new driver that will issue commands using the wire protocol.

"""

capabilities = {}

if options is not None:

capabilities = options.to_capabilities()

if desired_capabilities is not None:

if not isinstance(desired_capabilities, dict):

raise WebDriverException("Desired Capabilities must be a dictionary")

else:

capabilities.update(desired_capabilities)

if proxy is not None:

warnings.warn("Please use FirefoxOptions to set proxy",

DeprecationWarning, stacklevel=2)

proxy.add_to_capabilities(capabilities)

self.command_executor = command_executor

if type(self.command_executor) is bytes or isinstance(self.command_executor, str):

self.command_executor = RemoteConnection(command_executor, keep_alive=keep_alive)

self._is_remote = True

self.session_id = None

self.capabilities = {}

self.error_handler = ErrorHandler()

self.start_client()

if browser_profile is not None:

warnings.warn("Please use FirefoxOptions to set browser profile",

DeprecationWarning, stacklevel=2)

self.start_session(capabilities, browser_profile)

self._switch_to = SwitchTo(self)

self._mobile = Mobile(self)

self.file_detector = file_detector or LocalFileDetector()

从注释可以看出这里主要是:创建一个新的WebDriver实例,它将使用WebDriver协议来发送命令给浏览器。使用对应变量保存相关初始化需要的参数,然后开启一个会话(session)与webdriver建立通信,我们看看最重要的部分,也就是开启会话调用的函数:

def start_session(self, capabilities, browser_profile=None):

"""

Creates a new session with the desired capabilities.

"""

if not isinstance(capabilities, dict):

raise InvalidArgumentException("Capabilities must be a dictionary")

if browser_profile:

if "moz:firefoxOptions" in capabilities:

capabilities["moz:firefoxOptions"]["profile"] = browser_profile.encoded

else:

capabilities.update({'firefox_profile': browser_profile.encoded})

w3c_caps = _make_w3c_caps(capabilities)

parameters = {"capabilities": w3c_caps,

"desiredCapabilities": capabilities}

response = self.execute(Command.NEW_SESSION, parameters)

可以看到start_session()函数里面发送请求是:self.execute()函数,我们继续进入看看:

def execute(self, driver_command, params=None):

"""

Sends a command to be executed by a command.CommandExecutor.

"""

if self.session_id is not None:

if not params:

params = {'sessionId': self.session_id}

elif 'sessionId' not in params:

params['sessionId'] = self.session_id

params = self._wrap_value(params)

response = self.command_executor.execute(driver_command, params)

if response:

print("打印响应参数", json.dumps(response, indent=4))

self.error_handler.check_response(response)

response['value'] = self._unwrap_value(

response.get('value', None))

return response

# If the server doesn't send a response, assume the command was

# a success

return {'success': 0, 'value': None, 'sessionId': self.session_id}

通过源码的值它主要是通过CommandExecutor发送一个请求,这里我们把响应的结果打印到控制台看看,这里的响应返回了什么,新增一行输出代码如下:

我们在进入到self.command_executor.execute(driver_command, params)函数看看是怎么把请求发送出去的:

def execute(self, command, params):

"""

Send a command to the remote server.

Any path subtitutions required for the URL mapped to the command should be

included in the command parameters.

"""

command_info = self._commands[command]

assert command_info is not None, 'Unrecognised command %s' % command

path = string.Template(command_info[1]).substitute(params)

if hasattr(self, 'w3c') and self.w3c and isinstance(params, dict) and 'sessionId' in params:

del params['sessionId']

data = utils.dump_json(params)

url = '%s%s' % (self._url, path)

return self._request(command_info[0], url, body=data)

这里还是没有看到它到底是怎么把请求发送出去的,继续进入到self._request(command_info[0], url, body=data)函数:

def _request(self, method, url, body=None):

"""

Send an HTTP request to the remote server.

"""

LOGGER.debug('%s %s %s' % (method, url, body))

parsed_url = parse.urlparse(url)

headers = self.get_remote_connection_headers(parsed_url, self.keep_alive)

resp = None

if body and method != 'POST' and method != 'PUT':

body = None

print(f"请求参数:url: {url} \n body: {body} \n headers: {json.dumps(headers, indent=4)}")

if self.keep_alive:

resp = self._conn.request(method, url, body=body, headers=headers)

statuscode = resp.status

else:

http = urllib3.PoolManager(timeout=self._timeout)

resp = http.request(method, url, body=body, headers=headers)

终于到这里看到了它底层是通过urllib3库来发送http请求的,这里我们把请求的参数打印出来:

我们再次运行下面的代码,看看请求参数和响应结果是什么:

from selenium import webdriver

driver_path = 'E:\PycharmProjects\webUiTest\env\Scripts\chromedriver'

driver = webdriver.Chrome(executable_path=driver_path)

输出结果如下:

请求参数:url: http://127.0.0.1:59146/session

body: {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any", "goog:chromeOptions": {"extensions": [], "args": []}}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "goog:chromeOptions": {"extensions": [], "args": []}}}

headers: {

"Accept": "application/json",

"Content-Type": "application/json;charset=UTF-8",

"User-Agent": "selenium/3.141.0 (python windows)",

"Connection": "keep-alive"

}

打印响应参数 {

"value": {

"capabilities": {

"acceptInsecureCerts": false,

"browserName": "chrome",

"browserVersion": "127.0.6533.100",

"chrome": {

"chromedriverVersion": "127.0.6533.119 (bdef6783a05f0b3f885591e7d2c7b2aec1a89dea-refs/branch-heads/6533@{#1999})",

"userDataDir": "C:\\Users\\\u5218\u519b\\AppData\\Local\\Temp\\scoped_dir13212_999079333"

},

"fedcm:accounts": true,

"goog:chromeOptions": {

"debuggerAddress": "localhost:59154"

}, 拼多多帮助力入口

"networkConnectionEnabled": false,

"pageLoadStrategy": "normal",

"platformName": "windows",

"proxy": {},

"setWindowRect": true,

"strictFileInteractability": false,

"timeouts": {

"implicit": 0,

"pageLoad": 300000,

"script": 30000

},

"unhandledPromptBehavior": "dismiss and notify",

"webauthn:extension:credBlob": true,

"webauthn:extension:largeBlob": true,

"webauthn:extension:minPinLength": true,

"webauthn:extension:prf": true,

"webauthn:virtualAuthenticators": true

},

"sessionId": "34680a6d180d4c8f0a7225d00f92111f"

}

}

终于我们可以看到初始化建立会话是通过请求url: http://127.0.0.1:59146/session 然后返回sessionId,后续操作都会携带该sessionId请求,这样对应webdriver它才知道来自那个请求,从而实现会话保持,至此终于把会话建立了,后续就可以通过这个会话发送操作了。

3.2、发送操作

前言:通过上面的终于把会话建立了,现在我们就需要通过会话发送操作命令了,我们执行下面的代码看看这个过程是怎么的:

driver.get("https://www.baidu.com")

运行结果如下:

driver.get():做的事就是把get操作转换为对应的url地址,然后通过携带sessionId发送请求到webdriver服务端,也就是说driver.xxx()的每一个操作都对应了一个url地址,这里肯定有个映射关系来维持,进入源码查看不难找到在RemoteConnection这个类中维护了这样的关系:

好文链接,测试工具

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧