Selenium webdriver principle (b): How does Selenium manipulate the browser?

Source: Internet
Author: User
Tags documentation lenovo
Objective

The previous article, "How Selenium Webdriver is running", uses a simple example-taking a taxi to illustrate how selenium webdriver works, This article can be understood as an in-depth understanding of how selenium interacts with browser drivers, or it can be thought of as the 2.0 version of the article "How Selenium started the browser" written by the ethanol teacher.

Environment Preparation:

Python above 3.0
Selenium 3.0 min.
Browser Chrome
Browser driver Chromedriver
Interface Test Tools

Small compilation of the environment:

Python 3.6.4
Selenium 3.13
Browser: Chrome 68
Browser driver: Chromedriver 2.38
Interface test tool: Python requests

First, we run the following code block
#encoding:utf8from selenium import webdriverimport logginglogging.basicConfig(level=logging.DEBUG)dr = webdriver.Chrome()dr.implicitly_wait(10)#打开深圳-逸遥 博客园首页dr.get('www.cnblogs.com/snailrunning')#定位深圳-逸遥 第一篇博文标题el = dr.find_element_by_css_selector('.postTitle a')#点击第一篇博文标题el.click()
Operation Result:
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:chromeoptio NS ": {" 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": "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 
From the above code to run the results, we can draw the following conclusions
    • For each Selenium command, an HTTP request is created and sent to the browser driver
    • The execution results of each command are returned to the automation code
    • Chromedirver open a browser when creating session
    • Selenium code and browser-driven interactions are created based on Chromedriver SessionID
Article here, a lot of test students will be dizzy, it's OK, we now based on the results of the above to disassemble the request interface and return, and we use the interface tool to simulate the selenium automation code to manipulate browser 1, start the browser interface
Request method: Post request Url:http://127.0.0.1:4102/session request body: {"capabilities": {"Firstmatch": [{}], "Alwaysmatch": {" Browsername ":" Chrome "," PlatformName ":" Any "," goog:chromeoptions ": {" extensions ": []," args ": []}}," Desiredcapabi Lities ": {" Browsername ":" Chrome "," Version ":" "," platform ":" Any "," goog:chromeoptions ": {" extensions ": []," args ": []} }} return 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 Opening Chomedriver

Starting ChromeDriver 2.38.552522   开启ChromeDriver 版本号2.38.552522(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 9515 监听的端口是9515Only local connections are allowed. ; 只允许本地链接

1.2 Construction Requests

请求方式 :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 Sending requests to chromedriver using Python requests

#encoding:utf8import requestssession_url = 'http://localhost:9515/session'session_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())
The Chrome browser is now open

1.4 View return Results

{"SessionId": "b2801b5dc58b15e76d0d3295b04d295c", "status": 0, "value": {"Acceptinsecurecerts": false, "Acceptsslcerts": false, "applicationcacheenabled": false, "browserconnectionenabled": false, " Browsername ":" Chrome "," Chrome ": {" chromedriverversion ":" 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86 763DC9DCB) "," Userdatadir ":" c:\\users\\lenovo\\appdata\\local\\temp\\scoped_dir1792_5142 "}," CS        Sselectorsenabled ": True," databaseenabled ": false," handlesalerts ": True," Hastouchscreen ": false, "Javascriptenabled": True, "locationcontextenabled": True, "mobileemulationenabled": false, "NA Tiveevents ": True," networkconnectionenabled ": false," pageloadstrategy ":" Normal "," platform ":" Wind oWS NT "," rotatable ": false," SetWindowRect ": True," Takesheapsnapshot ": true," Takesscreensho T ": true," unexpeCtedalertbehaviour ":", "Version": "68.0.3440.106", "webstorageenabled": True}} 


2, open the Shenzhen-yi Yao's Blog park

2.1 Construction Requests

请求方式 :POST请求地址 :http://localhost:9515/session/:sessionId/url注意: 上述地址中的 ":sessionId"要用启动浏览器的请求返回结果中的sessionId的值例如:我刚刚发送请求,启动浏览器,返回结果中"sessionId": "b2801b5dc58b15e76d0d3295b04d295c"  然后我构造 导航到"深圳-逸遥的博客园"的请求地址请求地址:http://localhost:9515/session/b2801b5dc58b15e76d0d3295b04d295c/url请求body :{"url": "www.cnblogs.com/snailrunning", "sessionId": "b2801b5dc58b15e76d0d3295b04d295c"}

2.2 Sending requests to chromedriver using Python requests

#encoding:utf8import requestsurl = 'http://localhost:9515/session/b2801b5dc58b15e76d0d3295b04d295c/url'pars = {"url": "www.cnblogs.com/snailrunning", "sessionId": "b2801b5dc58b15e76d0d3295b04d295c"}r = requests.post(url,json=pars)print(r.json())
The browser opens the "Shenzhen-Yat-yao" blog Park

2.3 Viewing Request return results

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


3, positioning "Shenzhen-yi Yao" the title of the first blog post

3.1 Construction Requests

请求方式 :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 Sending requests to chromedriver using Python requests

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

3.3 View the results returned by the request

{'sessionId': 'b2801b5dc58b15e76d0d3295b04d295c', 'status': 0, 'value': {'ELEMENT': '0.11402119390850629-1'}}
    • return {' ELEMENT ': ' 0.11402119390850629-1 '} in the result
    • Official documentation is called: the Webelement JSON object of the found element, representing the DOM element on the page, and the value of the server assigned to element is opaque (random) The value of this element is used in all subsequent commands issued against that element.


4, click "Shenzhen-yi Yao" blog The title of the first post

4.1 Construction Requests

请求方式 :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 Sending requests to chromedriver using Python requests

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


The browser navigates to the first post of "Shenzhen-yi Yao" homepage

4.3 View the results returned by the request

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


Fry the old rice at the end of the article
    • For each Selenium command, an HTTP request is created and sent to the browser driver
    • The execution results of each command are returned to the automation code
    • The response status code status equals 0, which means the command execution succeeds
    • Chromedirver open a browser when creating session
    • Selenium code and browser-driven interactions are created based on Chromedriver SessionID


Interface documentation associated with the above operations--selenium Webdriver Jsonwireprotocol

Webdriver Jsonwireprotocol Basic Terms and concepts

Request Response Description

Launch Browser, create SessionID

Navigate to the specified URL

Element positioning

ELEMENT Click action


Reference articles

How ethanol-Selenium is the start of the browser

Recommended Reading

How ethanol-Selenium is the start of the browser

Shenzhen-yi Yao-Selenium webdriver principle (a): How does Selenium webdriver work?




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.