当前位置: 首页 > 文档资料 > Selenium 中文文档 >

selenium-webdriver(python) (十四) — webdriver原理

优质
小牛编辑
127浏览
2023-12-01

之前看乙醇视频中提到,selenium 的ruby 实现有一个小后门,在代码中加上$DEBUG=1 ,再运行脚本的过程中,就可以看到客户端请求的信息与服务器端返回的数据;觉得这个功能很强大,可以帮助理解webdriver的运行原理。

后来查了半天,python并没有提供这样一个方便的后门,不过我们可以通过代理的方式获得这些交互信息;

一、需要安装java 虚拟机与selenium-server-standalone ,参考 《selenium + python自动化测试环境搭建》第7、8操作:

二、通过下面命令启动服务:

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

在命令结尾加 >d:\log.txt 可以将命令信息存入文件,但信息很少。

然后运行下面的自动化脚本:

#coding = utf-8import timefrom selenium import webdriverfrom 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原理:

1. WebDriver 启动目标浏览器,并绑定到指定端口。该启动的浏览器实例,做为web driver的remote server。

2. Client 端通过CommandExcuter 发送HTTPRequest 给remote server 的侦听端口(通信协议: the webriver wire protocol)

3. Remote server 需要依赖原生的浏览器组件(如:IEDriver.dll,chromedriver.exe),来转化转化浏览器的native调用。

查看命令提示符下的运行日志:

咋一看很乱,慢慢分析一下就发现很有意思!结合上面的脚本分析

--------------------------------------------------------------------------------------->java -jar selenium-server-standalone-.,  :::: INFO - Java: Oracle Corporation -:: INFO - OS: Windows XP :: INFO - v2., with Core v2.:: INFO - RemoteWebDriver instances should connect to: http::/wd/:: INFO - Version Jetty/:: INFO - Started HttpContext[/selenium-server/driver,/selenium-/:: INFO - Started HttpContext[/selenium-server,/selenium-:: INFO - Started HttpContext[/,/:: INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@:: INFO - Started HttpContext[/wd,/:: INFO - Started SocketListener on .::: INFO ---------------------------------------------------------------------------------------:: INFO - Executing: [new session: {platform=ANY, javascriptEnabled==chrome, version=}] at URL: /:: INFO - Creating a new session  Capabilities [{platform==, browserName=chrome, version=][INFO]: received Webriver request: GET /][INFO]: sending Webriver response: : : : : : : ][INFO]: received Webriver request: POST /: : : : ][INFO]: Launching chrome: ettings\Application ata\Google\Chrome\Application\chrome.exe-port= --no-first-run --enable-logging --logging-level= --user-data-dir=\OCUME~\AMINI~\LOCALS~\Temp\scoped_dir1808_7550C:\OCUME~~\LOCALS~\Temp\scoped_dir1808_26821\internals data:text/html;charset=utf-][INFO]: sending Webriver response: ][INFO]: received Webriver request: GET /session/][INFO]: sending Webriver response: : : : : : : : : : : : : : : : : : : :: INFO - Done: /:: INFO -/session/ac5b2c71-5b1a-469e-814c-:: INFO - Done: /session/ac5b2c71-5b1a-469e-814c-:: INFO - Executing: [get: http:b2c71-5b1a-469e-814c-fdd09a2061fc/][INFO]: received Webriver request: POST /session//: ][INFO]: waiting ][INFO]: done waiting ][INFO]: waiting ][INFO]: done waiting ][INFO]: sending Webriver response: : : : :: INFO - Done: /session/ac5b2c71-5b1a-469e-814c-fdd09a2061fc/--------------------------------------------------------------------------------------:: INFO - Executing: [find element: By.name: q] at URL: /session/-5b1a-469e-814c-fdd09a2061fc/][INFO]: received Webriver request: POST /session//: : ][INFO]: waiting ][INFO]: done waiting ][INFO]: waiting ][INFO]: done waiting ][INFO]: sending Webriver response: : : : :: INFO - Done: /session/ac5b2c71-5b1a-469e-814c-fdd09a2061fc/:: INFO - Executing: [send keys: //ac5b2c71-5b1a-469e-814c-fdd09a2061fc/element//][INFO]: received Webriver request: POST /session//element/:/: : [ , , , , ][INFO]: waiting ][INFO]: done waiting ][INFO]: waiting ][INFO]: done waiting ][INFO]: sending Webriver response: : : : :: INFO - Done: /session/ac5b2c71-5b1a-469e-814c-fdd09a2061fc/element//:: INFO - Executing: [find element: By.name: q] at URL: /session/-5b1a-469e-814c-fdd09a2061fc/][INFO]: received Webriver request: POST /session//: : ][INFO]: waiting ][INFO]: done waiting ][INFO]: waiting ][INFO]: done waiting ][INFO]: sending Webriver response: : : : :: INFO - Done: /session/ac5b2c71-5b1a-469e-814c-fdd09a2061fc/:: INFO - Executing: [send keys: /session/ac5b2c71-5b1a-469e-814c-fdd09a2061fc/element//][INFO]: received Webriver request: POST /session//element/:/: : [ , , , , , , , , ][INFO]: waiting ][INFO]: done waiting ][INFO]: waiting ][INFO]: done waiting ][INFO]: sending Webriver response: : : : :: INFO - Done: /session/ac5b2c71-5b1a-469e-814c-fdd09a2061fc/element//:: INFO - Executing: [close window] at URL: /session/ac5b2c71-5b1a--814c-fdd09a2061fc/][INFO]: received Webriver request: ELETE /session//~\ADMINI~\LOCALS~\Temp\scoped_dir1808_7550  C:\DOCUME~~\LOCALS~][INFO]: sending Webriver response: : : : :: INFO - Done: /session/ac5b2c71-5b1a-469e-814c-fdd09a2061fc/window

request 请求 / response 应答

一次请求会对应一次应答

POST/GET 是请求(request)两种类型;关于两种请求方式的类别参考其它资料

200 、203 是 HTTP请求返回的状态码,200表示成功;

sessionid :每一个访问服务器的客户端,都要先得到服务器端分配的一个sessionid ,就像通行证一样,只有得到sessionid的客户端才能向服务器请求想要的数据。

  其它还包括操作系统版本,浏览器类型、URL、字符类型等非常详细的记录。熟悉HTTP ,了解TCP 的三次握手四次挥手,相信你对浏览器的交互与webdriver原理会有更深入的认识。