综合性项目, 包含一系列的工具和库, 可以用于web自动化、爬虫、刷单、抢票。。。
主要特点:
selenium 三大组件
元素:python -> selenium ->驱动 -> 浏览器
pip install selenium
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.quit()
pip config set global.extra-index-url https://beifan:[email protected]/simple
pip config set global.index-url = https://mirrors.aliyun.com/pypi/simple
pip install webdriver-helper
pip install packaging
# 如加 -u 表示自动升级到最新版
from webdriver_helper.driver import get_webdriver
driver = get_webdriver()
driver.get("http://baidu.com")
driver.quit()
区别:
- webdriver.chorme() 从path中寻找浏览器驱动, 找不到报错
- get_webdriver() 从.wdm目录中寻找浏览器驱动, 找不到自动下载
找到驱动之后,一切用法一致
大牛推荐学习方式:
python web自动化测试控制链路:
python -> webDriver、webElement -> HTTPAPI ->chromedriver -> chrome
postman
控制步骤:
1.启动浏览器
2.跳转到百度
3. 在输入框输入: xxx
4. 关闭浏览器
请求
post 127.0.0.1:9515/session
{
"capabilities": {
"alwaysMatch": {
"cloud:user": "alice",
"cloud:password": "hunter2",
"platformName": "windows"
},
"firstMatch": [
{"browserName": "chrome"}
]
}
}
**返回 **
{
"value": {
"capabilities": {
"acceptInsecureCerts": false,
"browserName": "chrome",
"browserVersion": "103.0.5060.114",
"chrome": {
"chromedriverVersion": "103.0.5060.53 (a1711811edd74ff1cf2150f36ffa3b0dae40b17f-refs/branch-heads/5060@{#853})",
"userDataDir": "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\scoped_dir5996_1859030467"
},
"goog:chromeOptions": {
"debuggerAddress": "localhost:63829"
},
"networkConnectionEnabled": false,
"pageLoadStrategy": "normal",
"platformName": "windows",
"proxy": {},
"setWindowRect": true,
"strictFileInteractability": false,
"timeouts": {
"implicit": 0,
"pageLoad": 300000,
"script": 30000
},
"unhandledPromptBehavior": "dismiss and notify",
"webauthn:extension:credBlob": true,
"webauthn:extension:largeBlob": true,
"webauthn:virtualAuthenticators": true
},
"sessionId": "4128af60f25ea27569ee71702436dc86"
}
}
POST 127.0.0.1:9515/session/4128af60f25ea27569ee71702436dc86/url
body {"url": "https://baidu.com"}
respones
{
"value": null
}
POST 127.0.0.1:9515/session/8206d92324240cb8726bdce1ac36e6fe/element
body {"using": "xpath","value":"//*[@id='kw']"}
respones
{
"value": {
"element-6066-11e4-a52e-4f735466cecf": "317a611e-be72-46bb-b690-f6e5f5610b3a"
}
}
POST 127.0.0.1:9515/session/8206d92324240cb8726bdce1ac36e6fe/element/317a611e-be72-46bb-b690-f6e5f5610b3a/value
body {"type": "typing","text":"xxxx"}
respones
{
"value": null
}
DELETE 127.0.0.1:9515/session/8206d92324240cb8726bdce1ac36e6fe
respones
{
"value": null
}
结论
selenium 封装了对接口的调用, 使用面向对象的方式, 通过对webDriver 、webElement实例方法调用完成复制的、关联性的浏览器控制。 换个角度想,selenium 完成对接口的封装