一、Selenium环境-1
1、Selenium工作原理
- client代表写脚本的地方。
- 通过driver,client第一次启动抵达浏览器。
- client与浏览器创建会话session(是唯一的)。
- 浏览器解析并接收client的指令。
- 浏览器执行指令操作。
- 浏览器将执行结果返回给client。
2、软件版本
- python-3.6.5-amd64-webinstall.exe
- Python Release Python 3.6.5 | Python.org
- customize installation
- 选pip(扩展包的管理工具)
- 安装至D:\Python
- 电脑-高级系统设置-环境变量-系统变量-path-添加D:\Python
- 电脑-高级系统设置-环境变量-系统变量-path-添加D:\Python\Scripts
- 检测安装及环境配置成功(进入cmd,输入python即可,查看版本)
C:\Users\jieqiong>python Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> quit() C:\Users\jieqiong>
- selenium
- pip install selenium (进入cmd,输入该命令即可)
- 检测安装成功(先输入python,接着输入from selenium import webdriver)
C:\Users\jieqiong>pip install selenium Collecting selenium Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB) 100% |████████████████████████████████| 911kB 59kB/s Collecting urllib3 (from selenium) Downloading https://files.pythonhosted.org/packages/d1/cb/4783c8f1a90f89e260dbf72ebbcf25931f3a28f8f80e2e90f8a589941b19/urllib3-1.26.11-py2.py3-none-any.whl (139kB) 100% |████████████████████████████████| 143kB 39kB/s Installing collected packages: urllib3, selenium Successfully installed selenium-3.141.0 urllib3-1.26.11 You are using pip version 9.0.3, however version 22.2.2 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. C:\Users\jieqiong>python Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from selenium import webdriver >>>
- visual studio code
- 创建文件目录D:\imooc\selenium
- 创建文件start_browser.py
- 在https://npm.taobao.org/mirrors/chromedriver/下载和浏览器相匹配的驱动,放置在D:\Python中
- 按Windows键或单击“开始”按钮,在文本字段中键入“ pwsh”,然后在结果中出现PowerShell 7时按Enter或单击“打开”。即看到PowerShell 的版本号7.2.6。
- 点击菜单栏中的运行-启动调试。
- 在下面的“终端”,输入python .\start_browser.py
- 服务器自动启动
#coding=utf-8 from selenium import webdriver driver = webdriver.Chrome() driver.get("http://www.baidu.com")
PS D:\imooc\selenium> python .\start_browser.py DevTools listening on ws://127.0.0.1:54202/devtools/browser/85c9f7af-fd9a-47e9-8cc8-1da9c8874793 PS D:\imooc\selenium> [32588:73184:0812/153410.324:ERROR:device_event_log_impl.cc(214)] [15:34:10.325] USB: usb_service_win.cc:415 Could not read device interface GUIDs: 系统找不到指定的文件。 (0x2) [32588:73184:0812/153410.328:ERROR:device_event_log_impl.cc(214)] [15:34:10.328] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F) [32588:73184:0812/153410.337:ERROR:device_event_log_impl.cc(214)] [15:34:10.337] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F) [32588:73184:0812/153410.337:ERROR:device_event_log_impl.cc(214)] [15:34:10.337] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F) [32588:73380:0812/153505.940:ERROR:util.cc(127)] Can't create base directory: C:\Program Files\Google\GoogleUpdater
PS D:\imooc\selenium> python .\start_browser.py DevTools listening on ws://127.0.0.1:60455/devtools/browser/f8a521ec-77d5-4416-b307-d7cc859467e2