Selenium-webdriver (Python) (14) Introduction to Webdriver Principle

Source: Internet
Author: User
Tags http request httpcontext log


Before watching ethanol video mentioned that selenium's Ruby implementation has a small back door, add $debug=1 in the code, and then run the script, you can see the client requested information and server-side data returned; Think this is a powerful feature, can help to understand how Webdriver works.



After a long search, Python did not provide such a convenient backdoor, but we can obtain these interactive information through the proxy way;



First, need to install Java Virtual machine and Selenium-server-standalone, refer to "Selenium + Python Automated test environment Build" 7th, 8 operation:



Second, start the service with the following command:



C:\selenium>java-jar Selenium-server-standalone-2.33.0.jar



At the end of the command, >d:\log.txt can store the command information in a file, but with very little information.



Then run the following Automation script:


#coding = utf-8
    
import time from
Selenium import webdriver from
selenium.webdriver.common.desired_ Capabilities Import desiredcapabilities
    
driver = webdriver. Remote (desired_capabilities=desiredcapabilities.chrome)
driver.get ("http://www.youdao.com")
Driver.find_element_by_name ("Q"). Send_keys ("Hello")
driver.find_element_by_name ("Q"). Send_keys ("key. ENTER ")
    
Driver.close ()


Webdriver principle:



1. Webdriver launches the target browser and binds to the specified port. The Startup browser instance, as a remote server for the Web driver.



2. The client side sends the listening port HttpRequest to the remote server via Commandexcuter (Communication protocol: the Webriver Wire protocol)



3. Remote server needs to rely on native browser components (such as: Iedriver.dll,chromedriver.exe) to transform the native invocation of the browser.



To view the run log at the command prompt:



I look very messy, and slowly analyze it found very interesting! Combining the above scripting analysis



See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/


-------------------------------------------------- -------------------------------------
Start the agent into the listening state
C: \ selenium> java -jar selenium-server-standalone-2.33.0.jar
August 22, 2013 10:19:48 am org.openqa.grid.selenium.GridLauncher main
INFO: Launching a standalone server
10: 19: 48.734 INFO-Java: Oracle Corporation 23.21-b01
10: 19: 48.734 INFO-OS: Windows XP 5.1 x86
10: 19: 48.734 INFO-v2.33.0, with Core v2.33.0. Built from revision 4e90c97
10: 19: 48.843 INFO-RemoteWebDriver instances should connect to: http: //127.0.0.
1: 4444 / wd / hub
10: 19: 48.843 INFO-Version Jetty / 5.1.x
10: 19: 48.843 INFO-Started HttpContext [/ selenium-server / driver, / selenium-server
/ driver]
10: 19: 48.843 INFO-Started HttpContext [/ selenium-server, / selenium-server]
10: 19: 48.843 INFO-Started HttpContext [/, /]
10: 19: 48.890 INFO-Started org.openqa.jetty.jetty.servlet.ServletHandler@176343
e
10: 19: 48.890 INFO-Started HttpContext [/ wd, / wd]
10: 19: 48.906 INFO-Started SocketListener on 0.0.0.0:4444
10: 19: 48.906 INFO-Started org.openqa.jetty.jetty.Server@388c74
-------------------------------------------------- ------------------------------------
    
Create new session
10: 20: 38.593 INFO-Executing: [new session: {platform = ANY, javascriptEnabled = tr
ue, browserName = chrome, version =)] at URL: / session)
10: 20: 38.593 INFO-Creating a new session for Capabilities [(platform = ANY, java
scriptEnabled = true, browserName = chrome, version =}]
    
webdrivr sends a request via GET
[0.921] [INFO]: received Webriver request: GET / status
    
Returns a response to webdrver with a return code of 200 indicating success
[0.921] [INFO]: sending Webriver response: 200 {
   "sessionId": "",
   "status": 0,
   "value": {
      "build": {
         "version": "alpha"
      },
      "os": {
         "arch": "x86",
         "name": "Windows NT",
         "version": "5.1 SP3"
      }
   }
}
    
webdriver sends the request again in POST mode and starts the browser related information
[0.984] [INFO]: received Webriver request: POST / session {
   "desiredCapabilities": {
      "browserName": "chrome",
      "javascriptEnabled": true,
      "platform": "ANY",
      "version": ""
   }
}
[0.984] [INFO]: Launching chrome: "C: \ ocuments and Settings \ Administrator \ Local S
ettings \ Application ata \ Google \ Chrome \ Application \ chrome.exe "--remote-debugging
-port = 4223 --no-first-run --enable-logging --logging-level = 1 --user-data-dir = "C:
\ OCUME ~ 1 \ AMINI ~ 1 \ LOCALS ~ 1 \ Temp \ scoped_dir1808_7550 "--load-extension =" C: \ OCUME ~ 1
\ AMINI ~ 1 \ LOCALS ~ 1 \ Temp \ scoped_dir1808_26821 \ internal "--ignore-certificate-error
s data: text / html; charset = utf-8,
[1.773] [INFO]: sending Webriver response: 303
    
webdriver requests again with the GET method, which adds session information
[1.778] [INFO]: received Webriver request: GET / session / 32b33aa585ccbbf7ba7853588
2852af3
    
The server first parses the sesssionID and confirms that it is called by Selenium and the URL to be accessed.
[1.779] [INFO]: sending Webriver response: 200 {
   "sessionId": "32b33aa585ccbbf7ba78535882852af3",
   "status": 0,
   "value": {
      "acceptSslCerts": true,
      "applicationCacheEnabled": false,
      "browserConnectionEnabled": false,
      "browserName": "chrome",
      "chrome": {
         "chromedriverVersion": "2.0"
      },
      "cssSelectorsEnabled": true,
      "databaseEnabled": true,
      "handlesAlerts": true,
      "javascriptEnabled": true,
      "locationContextEnabled": true,
      "nativeEvents": true,
      "platform": "Windows NT",
      "rotatable": false,
      "takesScreenshot": true,
      "version": "27.0.1453.116",
      "webStorageEnabled": true
   }
}
10: 20: 40.640 INFO-Done: / session
10: 20: 40.640 INFO-Executing: org.openqa.selenium.remote.server.handler.GetSess
ionCapabilities @ 14cf7a1 at URL: / session / ac5b2c71-5b1a-469e-814c-fdd09a2061fc)
10: 20: 40.640 INFO-Done: / session / ac5b2c71-5b1a-469e-814c-fdd09a2061fc
10: 20: 40.656 INFO-Executing: [get: http://www.youdao.com] at URL: / session / ac5
b2c71-5b1a-469e-814c-fdd09a2061fc / url)
    
webdriver is trying to request the server site from the server
[1.820] [INFO]: received Webriver request: POST / session / 32b33aa585ccbbf7ba785358
82852af3 / url {
   "url": "http://www.youdao.com"
}
[1.822] [INFO]: waiting for pending navigations ...
[1.829] [INFO]: done waiting for pending navigations
[2.073] [INFO]: waiting for pending navigations ...
[2.900] [INFO]: done waiting for pending navigations
    
Get server data response
[2.900] [INFO]: sending Webriver response: 200 {
   "sessionId": "32b33aa585ccbbf7ba78535882852af3",
   "status": 0,
   "value": null
}
10: 20: 41.734 INFO-Done: / session / ac5b2c71-5b1a-469e-814c-fdd09a2061fc / url
-------------------------------------------------- ------------------------------------
Next, send the information of the positioning input box.
10: 20: 41.734 INFO-Executing: [find element: By.name: q] at URL: / session / ac5b2
c71-5b1a-469e-814c-fdd09a2061fc / element)
[2.905] [INFO]: received Webriver request: POST / session / 32b33aa585ccbbf7ba785358
82852af3 / element {
   "using": "name",
   "value": "q"
}
[2.905] [INFO]: waiting for pending navigations ...
[2.905] [INFO]: done waiting for pending navigations
[2.922] [INFO]: waiting for pending navigations ...
[2.922] [INFO]: done waiting for pending navigations
    
Get server response
[2.922] [INFO]: sending Webriver response: 200 {
   "sess
ionId ":" 32b33aa585ccbbf7ba78535882852af3 ",
   "status": 0,
   "value": {
      "ELEMENT": "0.19427558477036655: 1"
   }
}
10: 20: 41.765 INFO-Done: / session / ac5b2c71-5b1a-469e-814c-fdd09a2061fc / element
10: 20: 41.765 INFO-Executing: [send keys: 0 org.openqa.selenium.support.events.
EventFiringWebDriver $ EventFiringWebElement @ a8215ba9, [h, e, l, l, o]] at URL: / s
ession / ac5b2c71-5b1a-469e-814c-fdd09a2061fc / element / 0 / value)
    
Write hello to the positioned input box
[2.936] [INFO]: received Webriver request: POST / session / 32b33aa585ccbbf7ba785358
82852af3 / element / 0.19427558477036655: 1 / value {
   "id": "0.19427558477036655: 1",
   "value": ["h", "e", "l", "l", "o"]
}
[2.936] [INFO]: waiting for pending navigations ...
[2.936] [INFO]: done waiting for pending navigations
[3.002] [INFO]: waiting for pending navigations ...
[3.002] [INFO]: done waiting for pending navigations
[3.002] [INFO]: sending Webriver response: 200 {
   "sessionId": "32b33aa585ccbbf7ba78535882852af3",
   "status": 0,
   "value": null
}
10: 20: 41.843 INFO-Done: / session / ac5b2c71-5b1a-469e-814c-fdd09a2061fc / element /
0 / value
    
Send a request to position the input box again
10: 20: 41.843 INFO-Executing: [find element: By.name: q] at URL: / session / ac5b2
c71-5b1a-469e-814c-fdd09a2061fc / element)
[3.006] [INFO]: received Webriver request: POST / session / 32b33aa585ccbbf7ba785358
82852af3 / element {
   "using": "name",
   "value": "q"
}
[3.006] [INFO]: waiting for pending navigations ...
[3.006] [INFO]: done waiting for pending navigations
[3.016] [INFO]: waiting for pending navigations ...
[3.016] [INFO]: done waiting for pending navigations
[3.016] [INFO]: sending Webriver response: 200 {
   "sessionId": "32b33aa585ccbbf7ba78535882852af3",
   "status": 0,
   "value": {
      "ELEMENT": "0.19427558477036655: 1"
   }
}
10: 20: 41.859 INFO-Done: / session / ac5b2c71-5b1a-469e-814c-fdd09a2061fc / element
10: 20: 41.859 INFO-Executing: [send keys: 0 org.openqa.selenium.support.events.
EventFiringWebDriver $ EventFiringWebElement @ a8215ba9, [k, e, y,., E, N, T, E, R]
] at URL: / session / ac5b2c71-5b1a-469e-814c-fdd09a2061fc / element / 0 / value)
    
Send an ENTER event request to the positioned input box
[3.021] [INFO]: received Webriver request: POST / session / 32b33aa585ccbbf7ba785358
82852af3 / element / 0.19427558477036655: 1 / value {
   "id": "0.19427558477036655: 1",
   "value": ["k", "e", "y", ".", "E", "N", "T", "E", "R"]
}
[3.021] [INFO]: waiting for pending navigations ...
[3.021] [INFO]: done waiting for pending navigations
[3.064] [INFO]: waiting for pending navigations ...
[3.064] [INFO]: done waiting for pending navigations
[3.064] [INFO]: sending Webriver response: 200 {
   "sessionId": "32b33aa585ccbbf7ba78535882852af3",
   "status": 0,
   "value": null
}
10: 20: 41.906 INFO-Done: / session / ac5b2c71-5b1a-469e-814c-fdd09a2061fc / element /
0 / value
10: 20: 41.906 INFO-Executing: [close window] at URL: / session / ac5b2c71-5b1a-469
e-814c-fdd09a2061fc / window)
[3.068] [INFO]: received Webriver request: ELETE / session / 32b33aa585ccbbf7ba78535
882852af3 / window
[WARNING: chrome_desktop_impl.cc (88)] chrome detaches, user should take care of d
irectory: C: \ DOCUME ~ 1 \ ADMINI ~ 1 \ LOCALS ~ 1 \ Temp \ scoped_dir1808_7550 and C: \ DOCUME ~ 1 \
ADMINI ~ 1 \ LOCALS ~ 1 \ Temp \ scoped_dir1808_26821
[5.318] [INFO]: sending Webriver response: 200 {
   "sessionId": "32b33aa585ccbbf7ba78535882852af3",
   "status": 0,
   "value": null
}
10: 20: 44.156 INFO-Done: / session / ac5b2c71-5b1a-469e-814c-fdd09a2061fc / window 

Request/Response Response



One request corresponds to one answer at a time



Post/get are the two types of requests (request);



200, 203 is the status code returned by HTTP request, 200 indicates success;



SessionID: Every client that accesses the server has to get a sessionid of the server-side allocation, just like a pass, only the client that gets SessionID can request the desired data from the server.



Other details include the operating system version, browser type, URL, character type, and so on. Familiar with HTTP, understand TCP's three handshake four times waved, I believe you to the browser interaction and Webdriver principle will have a deeper understanding.


Related Article

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.