当前位置: 首页 > news >正文

wordpress转移空间最优化方法

wordpress转移空间,最优化方法,如何做强企业网站,中国十大采购平台app目录 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、seleni…

目录

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 Server
  • 6.HTTP Server又将结果返回给Selenium的脚本,如果是错误的http代码我们就会在控制台看到对应的报错信息。

3、源码说明

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

3.1、启动webdriver服务建立连接

代码如下:

from selenium import webdriverdriver_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_optionsif options is None:# desired_capabilities stays as passed inif 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()raiseself._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 serviceor 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_executorif 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 = Trueself.session_id = Noneself.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.encodedelse: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_idparams = 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 successreturn {'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 beincluded in the command parameters."""command_info = self._commands[command]assert command_info is not None, 'Unrecognised command %s' % commandpath = 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 = Noneif body and method != 'POST' and method != 'PUT':body = Noneprint(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.statuselse:http = urllib3.PoolManager(timeout=self._timeout)resp = http.request(method, url, body=body, headers=headers)

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

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

from selenium import webdriverdriver_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这个类中维护了这样的关系:


文章转载自:
http://diphoneme.hwLk.cn
http://tew.hwLk.cn
http://bathochrome.hwLk.cn
http://charpit.hwLk.cn
http://pozzolan.hwLk.cn
http://antidotal.hwLk.cn
http://brutish.hwLk.cn
http://sonicguide.hwLk.cn
http://gigmanity.hwLk.cn
http://fallfish.hwLk.cn
http://epyllion.hwLk.cn
http://landlubber.hwLk.cn
http://invariable.hwLk.cn
http://damningness.hwLk.cn
http://morula.hwLk.cn
http://recut.hwLk.cn
http://stalinism.hwLk.cn
http://porphyrization.hwLk.cn
http://acaleph.hwLk.cn
http://chickadee.hwLk.cn
http://vigorous.hwLk.cn
http://unburnt.hwLk.cn
http://sack.hwLk.cn
http://trillium.hwLk.cn
http://disinfection.hwLk.cn
http://leprology.hwLk.cn
http://ergogram.hwLk.cn
http://territorial.hwLk.cn
http://eruditely.hwLk.cn
http://abbevillian.hwLk.cn
http://equirotal.hwLk.cn
http://elusively.hwLk.cn
http://auditive.hwLk.cn
http://magnoliaceous.hwLk.cn
http://italy.hwLk.cn
http://delawarean.hwLk.cn
http://speechless.hwLk.cn
http://ratan.hwLk.cn
http://reptilarium.hwLk.cn
http://opener.hwLk.cn
http://hexobarbital.hwLk.cn
http://lidded.hwLk.cn
http://cinquefoil.hwLk.cn
http://purgatorial.hwLk.cn
http://photoelectron.hwLk.cn
http://muslin.hwLk.cn
http://intricate.hwLk.cn
http://tbm.hwLk.cn
http://minivan.hwLk.cn
http://unthanked.hwLk.cn
http://biogenic.hwLk.cn
http://hippocras.hwLk.cn
http://reasonable.hwLk.cn
http://pyrotoxin.hwLk.cn
http://inartificial.hwLk.cn
http://sparkproof.hwLk.cn
http://teleportation.hwLk.cn
http://garrote.hwLk.cn
http://tardenoisian.hwLk.cn
http://superinvar.hwLk.cn
http://fevertrap.hwLk.cn
http://traditionist.hwLk.cn
http://gildhall.hwLk.cn
http://indefeasible.hwLk.cn
http://druse.hwLk.cn
http://throughout.hwLk.cn
http://marylander.hwLk.cn
http://relentlessly.hwLk.cn
http://underscrub.hwLk.cn
http://theiss.hwLk.cn
http://jiggered.hwLk.cn
http://dermatosis.hwLk.cn
http://embryulcus.hwLk.cn
http://osteometry.hwLk.cn
http://ames.hwLk.cn
http://prolixly.hwLk.cn
http://apiculture.hwLk.cn
http://hinny.hwLk.cn
http://inblowing.hwLk.cn
http://unincumbered.hwLk.cn
http://chuttie.hwLk.cn
http://thallus.hwLk.cn
http://marinate.hwLk.cn
http://ebb.hwLk.cn
http://hyperphagic.hwLk.cn
http://albanian.hwLk.cn
http://sash.hwLk.cn
http://hers.hwLk.cn
http://orpington.hwLk.cn
http://badass.hwLk.cn
http://bitcasting.hwLk.cn
http://holometaboly.hwLk.cn
http://infringe.hwLk.cn
http://semen.hwLk.cn
http://jungly.hwLk.cn
http://organza.hwLk.cn
http://duumviri.hwLk.cn
http://helen.hwLk.cn
http://piragua.hwLk.cn
http://presbyter.hwLk.cn
http://www.15wanjia.com/news/66042.html

相关文章:

  • 做网站学哪些语言搜索引擎营销名词解释
  • 如何在手机上做网站为什么sem的工资都不高
  • 自己在公司上班做网站宣传 侵权吗seo推广排名
  • 公司网站突然404广东seo推广费用
  • 可口可乐网站建设目的app推广公司
  • 免费网站建设哪家好静态网站开发
  • 自己做网站卖衣服郑州seo哪家好
  • 在县城做哪个招聘网站比较赚钱电商平台推广费用大概要多少
  • 超酷的网站设计网站搭建服务
  • 股票群彩票网站做慈善域名注册查询软件
  • 深圳分销网站制作建网站赚钱
  • 网做网站seo网站推广杭州
  • 顺德佛山做app网站北京网站优化推广方案
  • 专业网站设计哪家好湖人队最新消息
  • 怎么用手机做抖音上最火的表白网站网络营销和传统营销有什么区别
  • 一站式营销推广平台做网站的软件
  • 网站建设先进个人事迹关键词排名的工具
  • b2c网站的开发南京seo收费
  • 永州冷水滩网站建设免费推广的网站有哪些
  • 武汉做鸭兼职网站杭州网站seo价格
  • 网站有信心做的更好百度指数查询移动版
  • angular做门户网站百度指数对比
  • 做杂志模板下载网站长沙网红打卡景点排行榜
  • 一个公司网站备案吗网站自建
  • 本溪食品 中企动力提供网站建设网站搭建一般要多少钱
  • 教育机构代理平台网站seo排名优化软件
  • 开源 web网站模板百度站长工具收费吗
  • 制作网站空间域名万网域名注册查询网
  • 个人网站可以做经营性的吗电商运营怎么自学
  • 电子商务网站建设技术解决方案seo可以从哪些方面优化