Selenium是如何操纵浏览器的?

上一篇文章《selenium webdriver 是怎么运行的》用了一个简单的例子——搭出租车,形象地讲解selenium webdriver 是如何运行的,而这一篇文章可以理解为深入了解selenium是如何和浏览器驱动进行交互,也可以认为是乙醇老师写的《selenium是如何启动浏览器的》 文章的2.0版本 。

环境准备:

python 3.0以上
selenium 3.0以上
浏览器 Chrome
浏览器驱动 ChromeDriver
接口测试工具

小编的环境:

python 3.6.4
selenium 3.13
浏览器 :Chrome 68
浏览器驱动: ChromeDriver 2.38
接口测试工具:python requests

首先,我们运行下述代码块 #encoding:utf8 from selenium import webdriver import logging logging.basicConfig(level=logging.DEBUG) dr = webdriver.Chrome() dr.implicitly_wait(10) #打开深圳-逸遥 博客园首页 dr.get('https://www.cnblogs.com/snailrunning') #定位深圳-逸遥 第一篇博文标题 el = dr.find_element_by_css_selector('.postTitle a') #点击第一篇博文标题 el.click() 运行结果: DEBUG:selenium.webdriver.remote.remote_connection: POST :4102/session {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any", "goog:chromeOptions": {"extensions": [], "args": []}}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "goog:chromeOptions": {"extensions": [], "args": []}}} DEBUG:selenium.webdriver.remote.remote_connection: b'{"sessionId":"7cbbff953318267ef0089dc66f127051", "status":0, "value":{"acceptInsecureCerts":false,"acceptSslCerts":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"chromedriverVersion":"2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb)","userDataDir":"C:\\\\Users\\\\lenovo\\\\AppData\\\\Local\\\\Temp\\\\scoped_dir13812_4179"},"cssSelectorsEnabled":true,"databaseEnabled":false,"handlesAlerts":true,"hasTouchScreen":false,"javascriptEnabled":true,"locationContextEnabled":true,"mobileEmulationEnabled":false,"nativeEvents":true,"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platform":"Windows NT","rotatable":false,"setWindowRect":true,"takesHeapSnapshot":true,"takesScreenshot":true,"unexpectedAlertBehaviour":"","version":"68.0.3440.106","webStorageEnabled":true}}' DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection: POST :4102/session/7cbbff953318267ef0089dc66f127051/timeouts/implicit_wait {"ms": 10000.0, "sessionId": "7cbbff953318267ef0089dc66f127051"} DEBUG:selenium.webdriver.remote.remote_connection: b'{"sessionId":"7cbbff953318267ef0089dc66f127051","status":0,"value":null}' DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection: POST :4102/session/7cbbff953318267ef0089dc66f127051/url {"url": "https://www.cnblogs.com/snailrunning", "sessionId": "7cbbff953318267ef0089dc66f127051"} DEBUG:selenium.webdriver.remote.remote_connection: b'{"sessionId":"7cbbff953318267ef0089dc66f127051","status":0,"value":null}' DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection: POST :4102/session/7cbbff953318267ef0089dc66f127051/element {"using": "css selector", "value": ".postTitle a", "sessionId": "7cbbff953318267ef0089dc66f127051"} DEBUG:selenium.webdriver.remote.remote_connection: b'{"sessionId":"7cbbff953318267ef0089dc66f127051","status":0,"value":{"ELEMENT":"0.3612689441010788-1"}}' DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection: POST :4102/session/7cbbff953318267ef0089dc66f127051/element/0.3612689441010788-1/click {"id": "0.3612689441010788-1", "sessionId": "7cbbff953318267ef0089dc66f127051"} DEBUG:selenium.webdriver.remote.remote_connection: b'{"sessionId":"7cbbff953318267ef0089dc66f127051","status":0,"value":null}' DEBUG:selenium.webdriver.remote.remote_connection:Finished Request 从上述代码运行结果,我们可以得出以下结论

对于每个Selenium命令,都会创建一个HTTP请求并将其发送到浏览器驱动程序

每一个命令的执行结果都会返回给动化代码

ChromeDirver创建session时打开了浏览器

Selenium代码和浏览器驱动的交互都根据ChromeDriver创建的sessionId

文章到这里,很多测试的同学看了会头晕,没关系,我们现在先根据上述返回的结果来拆解一下请求的接口和返回,以及我们通过接口工具来模拟Selenium动化代码来操纵浏览器 1、启动浏览器接口 请求方式:post 请求url : :4102/session 请求body: {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any", "goog:chromeOptions": {"extensions": [], "args": []}}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "goog:chromeOptions": {"extensions": [], "args": []}}} 返回body : b'{"sessionId":"7cbbff953318267ef0089dc66f127051", "status":0, "value":{"acceptInsecureCerts":false,"acceptSslCerts":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"chromedriverVersion":"2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb)","userDataDir":"C:\\\\Users\\\\lenovo\\\\AppData\\\\Local\\\\Temp\\\\scoped_dir13812_4179"},"cssSelectorsEnabled":true,"databaseEnabled":false,"handlesAlerts":true,"hasTouchScreen":false,"javascriptEnabled":true,"locationContextEnabled":true,"mobileEmulationEnabled":false,"nativeEvents":true,"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platform":"Windows NT","rotatable":false,"setWindowRect":true,"takesHeapSnapshot":true,"takesScreenshot":true,"unexpectedAlertBehaviour":"","version":"68.0.3440.106","webStorageEnabled":true}}'

1.1 开启ChomeDriver

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zydyxj.html