Selenium 自动化测试实战笔记1

1. 安装 selenium

pip install selenium =3.11.0 # 安装指定版本

pip install selenium -U # 安装最新版本

pip show selenium # 查看当前版本

pip uninstall selenium # 卸载

报错解决1: 带上代理

pip install selenium -i http://mirrors.aliyun.com/pypi/simple/

报错解决2: 添加信任

pip3 install selenium==3.11.0 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

2. 第一个selenium 测试脚本

pycharm 设置 anaconda 的python 解释器

2.1安装谷歌浏览器驱动

2.1.1查找谷歌浏览器版本

2.1.2驱动下载

驱动下载; 【笔记】chromedriver下载与安装方法_chrome驱动_逸峰轻云的博客-CSDN博客

淘宝镜像:https://registry.npmmirror.com/binary.html?path=chromedriver/

官方镜像:http://chromedriver.storage.googleapis.com/index.html

验证安装

安装完成后,可以验证ChromeDriver是否正确安装并可用。请按照以下步骤进行:

打开终端或命令提示符窗口。

输入"chromedriver"(macOS / Linux)或"chromedriver.exe"(Windows)。

如果安装成功,将看到ChromeDriver的版本信息以及其他一些输出。


版权声明:本文为CSDN博主「逸峰轻云」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/mingfeng4923/article/details/130989513

下载的版本不匹配 :

如果没有完全一致的版本,下载最相近的也是可以的;

3. 脚本

复制代码
from selenium import webdriver
import time
driver=webdriver.Chrome()
#driver = webdriver.Firefox()
#driver = webdriver.Chrome()
# driver = webdriver.Edge()
url="http://www.baidu.com"
driver.get(url)
driver.find_element_by_id("kw").send_keys("Selenium") # 定位元素设置值
driver.find_element_by_id("su").click() # 定位 点击
time.sleep(10)
driver.quit()

4. 运行报错:

复制代码
:\software\python3\anconda3\python.exe E:/data/python/pythonProject/selenium_prj/first_selenium_test.py
Traceback (most recent call last):
  File "D:\software\python3\anconda3\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "D:\software\python3\anconda3\lib\subprocess.py", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "D:\software\python3\anconda3\lib\subprocess.py", line 1440, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\data\python\pythonProject\selenium_prj\first_selenium_test.py", line 3, in <module>
    driver=webdriver.Chrome()
  File "D:\software\python3\anconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
    self.service.start()
  File "D:\software\python3\anconda3\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home


Process finished with exit code 1

设置 驱动路径到环境变量或是将

把驱动文件所在目录添加到Path环境变量中(重启 pycharm)
或者直接放到Python安装目录,因为Python已添加到Path中

再次运行

相关推荐
moxiaoran575327 分钟前
uni-app学习笔记二十三--交互反馈showToast用法
笔记·学习·uni-app
scdifsn7 小时前
动手学深度学习12.7. 参数服务器-笔记&练习(PyTorch)
pytorch·笔记·深度学习·分布式计算·数据并行·参数服务器
jackson凌10 小时前
【Java学习笔记】SringBuffer类(重点)
java·笔记·学习
huangyuchi.11 小时前
【Linux】LInux下第一个程序:进度条
linux·运维·服务器·笔记·进度条·c/c++
大写-凌祁12 小时前
论文阅读:HySCDG生成式数据处理流程
论文阅读·人工智能·笔记·python·机器学习
Unpredictable22212 小时前
【VINS-Mono算法深度解析:边缘化策略、初始化与关键技术】
c++·笔记·算法·ubuntu·计算机视觉
傍晚冰川13 小时前
FreeRTOS任务调度过程vTaskStartScheduler()&任务设计和划分
开发语言·笔记·stm32·单片机·嵌入式硬件·学习
Love__Tay14 小时前
【学习笔记】Python金融基础
开发语言·笔记·python·学习·金融
半导体守望者15 小时前
ADVANTEST R3764 66 R3765 67爱德万测试networki connection programming网络程序设计手册
经验分享·笔记·功能测试·自动化·制造
有风南来15 小时前
算术图片验证码(四则运算)+selenium
自动化测试·python·selenium·算术图片验证码·四则运算验证码·加减乘除图片验证码