1.下载ChromeDriver,注意匹配goole版本,否则报错【此处有坑】
//google settings里的版本
Chrome is up to date
Version 122.0.6261.95 (Official Build) (64-bit)
chromeDriver最新版链接
下载后找到 chrome.exe 安装
相当于一个第三方插件,自动化执行在这个里面进行
2.aconda创建环境
Microsoft Windows [Version 10.0.22621.3155]
(c) Microsoft Corporation. All rights reserved.
C:\Users\suzisxue>conda env list
# conda environments:
#
base * C:\Users\suzisxue\Anaconda3
django_antd C:\Users\suzisxue\Anaconda3\envs\django_antd
django_dev C:\Users\suzisxue\Anaconda3\envs\django_dev
django_display C:\Users\suzisxue\Anaconda3\envs\django_display
drf_vue3 C:\Users\suzisxue\Anaconda3\envs\drf_vue3
fu_admin C:\Users\suzisxue\Anaconda3\envs\fu_admin
C:\Users\suzisxue>conda create -n django_run python==3.7
Collecting package metadata (current_repodata.json): done
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.10.3
latest version: 24.1.2
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: C:\Users\suzisxue\Anaconda3\envs\django_run
added / updated specs:
- python==3.7
The following NEW packages will be INSTALLED:
certifi pkgs/main/win-64::certifi-2022.12.7-py37haa95532_0
pip pkgs/main/win-64::pip-22.3.1-py37haa95532_0
python pkgs/main/win-64::python-3.7.0-hea74fb7_0
setuptools pkgs/main/win-64::setuptools-65.6.3-py37haa95532_0
vc pkgs/main/win-64::vc-14.2-h21ff451_1
vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.27.29016-h5e58377_2
wheel pkgs/main/win-64::wheel-0.38.4-py37haa95532_0
wincertstore pkgs/main/win-64::wincertstore-0.2-py37haa95532_2
Proceed ([y]/n)? y
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate django_run
#
# To deactivate an active environment, use
#
# $ conda deactivate
C:\Users\suzisxue>conda activate django_run
(django_run) C:\Users\suzisxue>python --version
Python 3.7.0
3.创建Python项目
C:\work\run_process>conda activate django_run
(django_run) C:\work\run_process>pip show selenium
Name: selenium
Version: 4.11.2
Summary:
Home-page: https://www.selenium.dev
Author:
Author-email:
License: Apache 2.0
Location: c:\users\suzisxue\anaconda3\envs\django_run\lib\site-packages
Requires: certifi, trio, trio-websocket, urllib3
Required-by:
(django_run) C:\work\run_process>pip install urllib3==1.26.15
Collecting urllib3==1.26.15
Downloading urllib3-1.26.15-py2.py3-none-any.whl (140 kB)
---------------------------------------- 140.9/140.9 kB 1.0 MB/s eta 0:00:00
Installing collected packages: urllib3
Attempting uninstall: urllib3
Found existing installation: urllib3 2.0.7
Uninstalling urllib3-2.0.7:
Successfully uninstalled urllib3-2.0.7
Successfully installed urllib3-1.26.15
用vscode创建python新项目
Python新项目的创建
注意对应conda创建的环境
4.执行并运行
from selenium import webdriver
import time
# 创建Chrome浏览器对象
browser = webdriver.Chrome()
# 打开目标网址
url = "https://www.baidu.com" # 将URL替换为要登录的网站地址
browser.get(url)
time.sleep(2) # 等待页面加载完成