一、selenium环境搭建
1、安装selenium
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple selenium
检验selenium是否安装成功,到Pycharm中 import一下,能正常导入说明安装成功。
2、下载对应的浏览器驱动
谷歌浏览器:http://chromedriver.storage.googleapis.com/index.html
https://npm.taobao.org/mirrors/chromedriver
其他浏览器:https://www.cnblogs.com/puhongjun/p/10232489.html
eg: 首先查看自己安装的google浏览器的版本
下载对应版本的浏览器驱动
然后将下载好的chromedriver 移动到/usr/local/bin目录下
打开终端执行命令:
前提:先切到你下载的chromedriver存放的目录下在执行如下命令。
sudo mv chromedriver /usr/local/bin
将chromedriver添加到环境变量中
打开:open .bash_profile
export PATH=$PATH:/usr/local/bin/chromedriver
source .bash_profile
3、支持语言
python、java、javaScript、ruby、c++
1、cs架构
脚本(客户端) ---- webdriver(对应浏览器驱动–服务器)---- web浏览器
2、本质
通过http协议进行通信
打开浏览器—手动启动webdriver服务器—发送post请求给webdriver操作浏览器
以打开百度为例:
from selenium import webdriver
import logging
import time
logging.basicConfig(level=logging.DEBUG)
# 启动浏览器驱动服务器
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(5)
driver.close()
日志:
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:50267/session {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any", "goog:chromeOptions": {"extensions": [], "args": []}}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "goog:chromeOptions": {"extensions": [], "args": []}}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1:50267
DEBUG:urllib3.connectionpool:http://127.0.0.1:50267 "POST /session HTTP/1.1" 200 797
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:50267/session/a302da3866b6357b06809f3a140d9bf4/url {"url": "https://www.baidu.com"}
DEBUG:urllib3.connectionpool:http://127.0.0.1:50267 "POST /session/a302da3866b6357b06809f3a140d9bf4/url HTTP/1.1" 200 14
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:DELETE http://127.0.0.1:50267/session/a302da3866b6357b06809f3a140d9bf4/window {}
DEBUG:urllib3.connectionpool:http://127.0.0.1:50267 "DELETE /session/a302da3866b6357b06809f3a140d9bf4/window HTTP/1.1" 200 12
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
四、手动模拟发送post请求打开浏览器,进入百度页面
import os
import requests
# 打开浏览器
url1 = "http://127.0.0.1:50267/session"
data1 = {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any",
"goog:chromeOptions": {"extensions": [], "args": []}}},
"desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY",
"goog:chromeOptions": {"extensions": [], "args": []}}}
res = requests.post(url=url1, json=data1)
print(res.json())
session_id = res.json()["sessionId"]
# 启动webdriver服务
os.system("/usr/local/bin/chromedriver")
# 访问百度地址
url2 = "http://127.0.0.1:50267/session/{}/url".format(session_id)
data2 = {"url": "https://baidu.com"}
res2 = requests.post(url=url2, json=data2)
print(res2.json())