標籤:

Selenium是如何操縱瀏覽器的?

Selenium是如何操縱瀏覽器的?

來自專欄測試圈TC2 人贊了文章

前言

上一篇文章《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:utf8from selenium import webdriverimport logginglogging.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 http://127.0.0.1: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 RequestDEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1: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 RequestDEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1: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 RequestDEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1: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 RequestDEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1: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 : http://127.0.0.1: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

Starting ChromeDriver 2.38.552522 開啟ChromeDriver 版本號2.38.552522(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 9515 監聽的埠是9515Only local connections are allowed. ; 只允許本地鏈接

1.2 構造請求

請求方式 :POST請求地址 :http://localhost:9515/session請求body :{"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any", "goog:chromeOptions": {"extensions": [], "args": []}}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "goog:chromeOptions": {"extensions": [], "args": []}}}

1.3 使用python requests 向 ChromeDriver發送請求

#encoding:utf8import requestssession_url = http://localhost:9515/sessionsession_pars = {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any", "goog:chromeOptions": {"extensions": [], "args": []}}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "goog:chromeOptions": {"extensions": [], "args": []}}}r_session = requests.post(session_url,json=session_pars)print(r_session.json())

此時Chrome瀏覽器被打開

1.4 查看返回結果

{ "sessionId": "b2801b5dc58b15e76d0d3295b04d295c", "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_dir1792_5142" }, "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 }}

2、打開深圳-逸遙的博客園

2.1 構造請求

請求方式 :POST請求地址 :http://localhost:9515/session/:sessionId/url注意: 上述地址中的 ":sessionId"要用啟動瀏覽器的請求返回結果中的sessionId的值例如:我剛剛發送請求,啟動瀏覽器,返回結果中"sessionId": "b2801b5dc58b15e76d0d3295b04d295c" 然後我構造 導航到"深圳-逸遙的博客園"的請求地址請求地址:http://localhost:9515/session/b2801b5dc58b15e76d0d3295b04d295c/url請求body :{"url": "https://www.cnblogs.com/snailrunning", "sessionId": "b2801b5dc58b15e76d0d3295b04d295c"}

2.2 使用python requests 向 ChromeDriver發送請求

#encoding:utf8import requestsurl = http://localhost:9515/session/b2801b5dc58b15e76d0d3295b04d295c/urlpars = {"url": "https://www.cnblogs.com/snailrunning", "sessionId": "b2801b5dc58b15e76d0d3295b04d295c"}r = requests.post(url,json=pars)print(r.json())

瀏覽器打開」深圳-逸遙「的博客園

2.3 查看請求返回結果

{sessionId: b2801b5dc58b15e76d0d3295b04d295c, status: 0, value: None}

3、定位」深圳-逸遙「第一篇博文的標題

3.1 構造請求

請求方式 :POST請求地址 :http://localhost:9515/session/:sessionId/element注意: 上述地址中的 ":sessionId"要用啟動瀏覽器的請求返回結果中的sessionId的值例如:我剛剛發送請求,啟動瀏覽器,返回結果中"sessionId": "b2801b5dc58b15e76d0d3295b04d295c" 然後我構造 查找頁面元素的請求地址請求地址:http://localhost:9515/session/b2801b5dc58b15e76d0d3295b04d295c/element請求body :{"using": "css selector", "value": ".postTitle a", "sessionId": "b2801b5dc58b15e76d0d3295b04d295c"}

3.2 使用python requests 向 ChromeDriver發送請求

#encoding:utf8import requestsurl = http://localhost:9515/session/b2801b5dc58b15e76d0d3295b04d295c/elementpars = {"using": "css selector", "value": ".postTitle a", "sessionId": "b2801b5dc58b15e76d0d3295b04d295c"}r = requests.post(url,json=pars)print(r.json())

3.3 查看請求返回的結果

{sessionId: b2801b5dc58b15e76d0d3295b04d295c, status: 0, value: {ELEMENT: 0.11402119390850629-1}}

  • 返回結果中的{ELEMENT: 0.11402119390850629-1}
  • 官方文檔稱為:找到的元素的WebElement JSON對象,表示頁面上的DOM元素,同時伺服器分配給ELEMENT的值是不透明的(隨機的) 這個ELEMENT的值會在針對該元素髮出的所有後續命令中使用。

4、點擊」深圳-逸遙「博客 第一篇博文的標題

4.1 構造請求

請求方式 :POST請求地址 :http://localhost:9515/session/:sessionId/element/:id/click注意: 上述地址中的 ":sessionId"要用啟動瀏覽器的請求返回結果中的sessionId的值:id 要用元素定位請求後返回ELEMENT的值例如:我剛剛發送請求,啟動瀏覽器,返回結果中"sessionId": "b2801b5dc58b15e76d0d3295b04d295c" 元素定位,返回ELEMENT的值"0.11402119390850629-1"然後我構造 點擊頁面元素的請求地址請求地址:http://localhost:9515/session/b2801b5dc58b15e76d0d3295b04d295c/element/0.11402119390850629-1/click請求body :{"id": "0.11402119390850629-1", "sessionId": "b2801b5dc58b15e76d0d3295b04d295c"}

4.2 使用python requests 向 ChromeDriver發送請求

#encoding:utf8import requestsurl = http://localhost:9515/session/b2801b5dc58b15e76d0d3295b04d295c/element/0.11402119390850629-1/clickpars ={"id": "0.5930642995574296-1", "sessionId": "b2801b5dc58b15e76d0d3295b04d295c"}r = requests.post(url,json=pars)print(r.json())

瀏覽器導航到「深圳-逸遙」首頁的第一篇博文

文章末尾再炒一下舊飯

  • 對於每個Selenium命令,都會創建一個HTTP請求並將其發送到瀏覽器驅動程序
  • 每一個命令的執行結果都會返回給自動化代碼
  • 響應狀態代碼 status 等於0 ,即表示命令執行成功
  • ChromeDirver創建session時打開了瀏覽器
  • Selenium代碼和瀏覽器驅動的交互都根據ChromeDriver創建的sessionId

附帶上述操作相關的介面文檔——selenium webdriver JsonWireProtocol

WebDriver JsonWireProtocol 基本術語和概念

請求響應說明

啟動瀏覽器,創建sessionId

導航指定url

元素定位

元素點擊操作

參考文章

乙醇 - selenium是如何啟動瀏覽器的

推薦閱讀

乙醇 - selenium是如何啟動瀏覽器的

深圳-逸遙 - Selenium WebDriver是怎麼工作的?

推薦閱讀:

ds4800+lvm信息丟失數據恢復方案
插畫心語:時光,濃淡相宜;人心,遠近相安 | 酷聽科技
峰會首創智能會議小程序:智能峰會,智能開!一路行程,手中抓!
矽谷預言帝KK《顛覆性科技》演講PPT
圖解 | Pycharm激活、配置以及快捷方式

TAG:科技 | Selenium |