0


【selenium】webdriver原理

webdriver原理

webdriver原理

webdriver是按照Server-Cilent 的经典模式设计的

#mermaid-svg-g6HwNd0ozpzXCc7w {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-g6HwNd0ozpzXCc7w .error-icon{fill:#552222;}#mermaid-svg-g6HwNd0ozpzXCc7w .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-g6HwNd0ozpzXCc7w .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-g6HwNd0ozpzXCc7w .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-g6HwNd0ozpzXCc7w .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-g6HwNd0ozpzXCc7w .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-g6HwNd0ozpzXCc7w .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-g6HwNd0ozpzXCc7w .marker{fill:#333333;stroke:#333333;}#mermaid-svg-g6HwNd0ozpzXCc7w .marker.cross{stroke:#333333;}#mermaid-svg-g6HwNd0ozpzXCc7w svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-g6HwNd0ozpzXCc7w .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-g6HwNd0ozpzXCc7w .cluster-label text{fill:#333;}#mermaid-svg-g6HwNd0ozpzXCc7w .cluster-label span{color:#333;}#mermaid-svg-g6HwNd0ozpzXCc7w .label text,#mermaid-svg-g6HwNd0ozpzXCc7w span{fill:#333;color:#333;}#mermaid-svg-g6HwNd0ozpzXCc7w .node rect,#mermaid-svg-g6HwNd0ozpzXCc7w .node circle,#mermaid-svg-g6HwNd0ozpzXCc7w .node ellipse,#mermaid-svg-g6HwNd0ozpzXCc7w .node polygon,#mermaid-svg-g6HwNd0ozpzXCc7w .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-g6HwNd0ozpzXCc7w .node .label{text-align:center;}#mermaid-svg-g6HwNd0ozpzXCc7w .node.clickable{cursor:pointer;}#mermaid-svg-g6HwNd0ozpzXCc7w .arrowheadPath{fill:#333333;}#mermaid-svg-g6HwNd0ozpzXCc7w .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-g6HwNd0ozpzXCc7w .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-g6HwNd0ozpzXCc7w .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-g6HwNd0ozpzXCc7w .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-g6HwNd0ozpzXCc7w .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-g6HwNd0ozpzXCc7w .cluster text{fill:#333;}#mermaid-svg-g6HwNd0ozpzXCc7w .cluster span{color:#333;}#mermaid-svg-g6HwNd0ozpzXCc7w div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-g6HwNd0ozpzXCc7w :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
http request

       Client->自动化脚本 
     

       Remote Server->任意浏览器 
     

调用过程

我们可以通过logging 开启debug模式来捕捉客户端–>服务端发送的请求
打开百度,输入selenium,并点击搜索功能

from selenium import webdriver
import logging

logging.basicConfig(level=logging.DEBUG)
driver = webdriver.Chrome()#打开百度页面
driver.get("https://www.baidu.com")# 在搜索框中输入selenium
driver.find_element_by_name("wd").send_keys("selenium")# 点击查询
driver.find_element_by_id("su").click()#关闭浏览器
driver.quit()

Debug日志如下:

webdriver 启动目标浏览器chrome 并绑定到指定端口:54127

DEBUG:selenium.webdriver.remote.remote_connection:POST http://localhost:54127/session {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any", "goog:chromeOptions": {"extensions": [], "args": []}}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "goog:chromeOptions": {"extensions": [], "args": []}}}

开启一个HTTP连接,访问https://www.baidu.com

DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): localhost:54127
DEBUG:urllib3.connectionpool:http://localhost:54127 "POST /session HTTP/1.1" 200 688
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://localhost:54127/session/f0c667fc89d08ed4451706a49ae81706/url {"url": "https://www.baidu.com"}

通过name查找搜索框

DEBUG:urllib3.connectionpool:http://localhost:54127 "POST /session/f0c667fc89d08ed4451706a49ae81706/url HTTP/1.1" 200 14
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://localhost:54127/session/f0c667fc89d08ed4451706a49ae81706/element {"using": "css selector", "value": "[name=\"wd\"]"}

在搜索框中输入文字selenium

DEBUG:urllib3.connectionpool:http://localhost:54127 "POST /session/f0c667fc89d08ed4451706a49ae81706/element HTTP/1.1" 200 88
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://localhost:54127/session/f0c667fc89d08ed4451706a49ae81706/element/cb40aab4-84fb-47cf-b4bc-669dc15e0c12/value {"text": "selenium", "value": ["s", "e", "l", "e", "n", "i", "u", "m"], "id": "cb40aab4-84fb-47cf-b4bc-669dc15e0c12"}
DEBUG:urllib3.connectionpool:http://localhost:54127 "POST /session/f0c667fc89d08ed4451706a49ae81706/element/cb40aab4-84fb-47cf-b4bc-669dc15e0c12/value HTTP/1.1" 200 14
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request

查找搜索按钮,并点击

DEBUG:selenium.webdriver.remote.remote_connection:POST http://localhost:54127/session/f0c667fc89d08ed4451706a49ae81706/element {"using": "css selector", "value": "[id=\"su\"]"}
DEBUG:urllib3.connectionpool:http://localhost:54127 "POST /session/f0c667fc89d08ed4451706a49ae81706/element HTTP/1.1" 200 88
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://localhost:54127/session/f0c667fc89d08ed4451706a49ae81706/element/afe9dc25-5369-4f80-8831-ee045cc16d26/click {"id": "afe9dc25-5369-4f80-8831-ee045cc16d26"}
DEBUG:urllib3.connectionpool:http://localhost:54127 "POST /session/f0c667fc89d08ed4451706a49ae81706/element/afe9dc25-5369-4f80-8831-ee045cc16d26/click HTTP/1.1" 200 14
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request

删除远程连接关闭浏览器

DEBUG:selenium.webdriver.remote.remote_connection:DELETE http://localhost:54127/session/f0c667fc89d08ed4451706a49ae81706 {}
DEBUG:urllib3.connectionpool:http://localhost:54127 "DELETE /session/f0c667fc89d08ed4451706a49ae81706 HTTP/1.1" 200 14
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
标签: selenium

本文转载自: https://blog.csdn.net/xubeiqiannian/article/details/140890015
版权归原作者 青格er 所有, 如有侵权,请联系我们删除。

“【selenium】webdriver原理”的评论:

还没有评论