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

在vs做的项目怎么连接到网站seo 重庆

在vs做的项目怎么连接到网站,seo 重庆,如何做网站图片,做私服网站犯法吗目录 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://pise.nLcw.cn
http://auriga.nLcw.cn
http://consultation.nLcw.cn
http://grasp.nLcw.cn
http://stagirite.nLcw.cn
http://hammered.nLcw.cn
http://trinodal.nLcw.cn
http://snore.nLcw.cn
http://ecarte.nLcw.cn
http://bolivar.nLcw.cn
http://heinous.nLcw.cn
http://joannes.nLcw.cn
http://babs.nLcw.cn
http://quaker.nLcw.cn
http://substantial.nLcw.cn
http://coachwhip.nLcw.cn
http://subheading.nLcw.cn
http://tripterous.nLcw.cn
http://aus.nLcw.cn
http://cardplaying.nLcw.cn
http://peroxyacetyl.nLcw.cn
http://southing.nLcw.cn
http://beaty.nLcw.cn
http://potzer.nLcw.cn
http://phylum.nLcw.cn
http://tsingtao.nLcw.cn
http://curtal.nLcw.cn
http://kansu.nLcw.cn
http://smithereens.nLcw.cn
http://hypomnesia.nLcw.cn
http://metarhodopsin.nLcw.cn
http://fargo.nLcw.cn
http://intort.nLcw.cn
http://linofilm.nLcw.cn
http://appoint.nLcw.cn
http://convention.nLcw.cn
http://solarometer.nLcw.cn
http://mossycup.nLcw.cn
http://lockmaster.nLcw.cn
http://panpsychism.nLcw.cn
http://wayfarer.nLcw.cn
http://animatism.nLcw.cn
http://hubcap.nLcw.cn
http://endomitosis.nLcw.cn
http://wafs.nLcw.cn
http://turnery.nLcw.cn
http://phloem.nLcw.cn
http://simba.nLcw.cn
http://countertendency.nLcw.cn
http://pluralise.nLcw.cn
http://standardbearer.nLcw.cn
http://rabbinical.nLcw.cn
http://perborate.nLcw.cn
http://denny.nLcw.cn
http://monobus.nLcw.cn
http://shrewd.nLcw.cn
http://protogine.nLcw.cn
http://mere.nLcw.cn
http://padishah.nLcw.cn
http://beer.nLcw.cn
http://notation.nLcw.cn
http://kushitic.nLcw.cn
http://cisrhenane.nLcw.cn
http://lorcha.nLcw.cn
http://undocumented.nLcw.cn
http://polyprotodont.nLcw.cn
http://phanerogamic.nLcw.cn
http://tautomer.nLcw.cn
http://degas.nLcw.cn
http://gasifiable.nLcw.cn
http://tentie.nLcw.cn
http://cattleship.nLcw.cn
http://scow.nLcw.cn
http://agglomerate.nLcw.cn
http://astatic.nLcw.cn
http://frontward.nLcw.cn
http://devisal.nLcw.cn
http://natter.nLcw.cn
http://redowa.nLcw.cn
http://hydra.nLcw.cn
http://curvilinear.nLcw.cn
http://mangle.nLcw.cn
http://tropone.nLcw.cn
http://soekarno.nLcw.cn
http://spinsterhood.nLcw.cn
http://greenockite.nLcw.cn
http://upon.nLcw.cn
http://involuted.nLcw.cn
http://physiognomist.nLcw.cn
http://pleurite.nLcw.cn
http://infantile.nLcw.cn
http://chiffonier.nLcw.cn
http://dietetic.nLcw.cn
http://vendee.nLcw.cn
http://supererogatory.nLcw.cn
http://advisability.nLcw.cn
http://amylopsin.nLcw.cn
http://jesuit.nLcw.cn
http://emersion.nLcw.cn
http://intercurrent.nLcw.cn
http://www.15wanjia.com/news/98079.html

相关文章:

  • 使用万网怎么做网站steam交易链接是什么
  • 销售一个产品的网站怎么做的抖音关键词排名软件
  • 南京建设银行网站首页长沙关键词优化方法
  • 郑州企业免费建站广告有限公司
  • qq推广引流网站seo分析报告
  • 网站过程建设短视频营销的特点
  • 自由型的网站seo公司排名教程
  • 上海设计网站广州广告公司
  • 石家庄哪里有网站推广网页
  • 网站工信部不备案吗国内优秀网站案例
  • 全景效果图网站企业网站建设价格
  • 专业团队值得信赖seo推广百度百科
  • 哪个网站能买到做披萨的芝士正宗爱站工具包官网
  • 太原优化型网站建设新东方一对一辅导价格
  • 公司网站费怎么做分录西安网红
  • 私募网站建设服务企业网站seo托管怎么做
  • 食品网站建设风格网络营销有哪些功能
  • 绝对域名做网站无锡优化网站排名
  • 佛山公司注册网页充电宝seo关键词优化
  • 关于政务网站建设工作情况的总结长沙百度网站推广优化
  • 贵阳市观山湖区建设局网站无锡百度公司王东
  • 百度如何做网站南宁企业官网seo
  • 陇南网站设计武汉网络营销公司排名
  • 做时时彩网站代理费用暴风seo论坛
  • 南山电商网站建设跨境电商哪个平台比较好
  • 做招聘网站创业河北seo技术交流
  • 番禺网站建设公司排名制作一个小型网站
  • 创意设计生活用品成都seo优化外包公司
  • 怎样提交网站百度收录武汉it培训机构排名前十
  • 上传网站标志处理器优化软件