【selenium:webdriver原理】

webdriver原理

webdriver原理

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

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
  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值