【自动化测试】selenium +Chrome driver环境配置
·
这里写自定义目录标题
前提条件,已安装了Python环境。
1. 安装 selenium
pip install selenium
2. 下载Chrome浏览器
下载完以后,打开Chrome,帮助-关于Google Chrome查看Chrome版本
3. 下载ChromeDriver
下载地址: Chrome for Testing availability.
前三位版本号一致即可。
3.1 解压出来的chromedriver.exe放到Python安装目录
Python地址通过where python命令查找:
C:\Users\ws>where python
C:\Users\ws\AppData\Local\Programs\Python\Python310\python.exe
我的 chromedriver.exe放到这里
C:\Users\ws\AppData\Local\Programs\Python\Python310
3.2 解压出来的chromedriver.exe放到Chrome安装目录
我的 chromedriver.exe 放到这里
C:\Program Files\Google\Chrome\Application
4. 将Chrome的路径放进环境变量
我的Chrome路径是放这个
C:\Program Files\Google\Chrome\Application
5. 代码验证
新建一个文本文件命名为test.py,复制如下内容:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# 创建 WebDriver 实例
driver = webdriver.Chrome()
# 打开百度网页
driver.get("https://www.baidu.com")
# 定位输入框并输入关键字
driver.find_element(By.ID, "kw").send_keys("CSDN")
# 点击[百度一下]搜索按钮
driver.find_element(By.ID, "su").click()
# 等待3秒
time.sleep(3)
# 关闭浏览器
driver.quit()
在cmd运行如下命令:
python C:\Users\ws\Downloads\test.py
如果能调起百度并自动搜索CSDN,恭喜你~
更多推荐

所有评论(0)