【记录】Python3|Selenium4 极速上手入门(Windows)
环境:Windows
版本:python3,selenium 4.11.2
写这个是方便自己重装电脑时重新装 Selenium,懒得每次都重新找链接。
文章目录
1 装
Chrome 和 Edge 或其他浏览器任选其一。
Chrome
首先,终端运行:
pip3 install selenium==4.11.2
官网下载Chrome:https://www.google.cn/intl/zh-CN/chrome/
安装好Chrome之后查看Chrome版本:chrome://settings/help
如果Chrome版本大于114,官网下载与Chrome版本对于的ChromeDriver:https://googlechromelabs.github.io/chrome-for-testing/,往下翻翻就能看到下载链接;
如果Chrome版本小于等于114,官网下载ChromeDriver链接:https://chromedriver.chromium.org/downloads。
解压下好的ChromeDriver.zip,把里面的exe拖出来,并记住放到了哪个路径。
写代码引入driver:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
s = Service("D:/software/chromedriver.exe")
driver = webdriver.Chrome(service=s)
结合Options使用的方式:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(
"user-agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'") # UA
s = Service("D:/software/chromedriver.exe")
driver = webdriver.Chrome(service=s, options=options)
Edge
首先,终端运行:
pip3 install selenium==4.11.2
官网下载Edge:https://www.microsoft.com/en-us/edge/download
安装好Edge之后查看Chrome版本:edge://settings/help
官网下载与Edge版本对于的webDriver:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
解压下好的edgeDriver.zip,把里面的exe拖出来,并记住放到了哪个路径。
from selenium import webdriver
from selenium.webdriver.edge.service import Service
s = Service('D:/software/msedgedriver.exe')
edge = webdriver.Edge(service=s)
结合Options使用的方式:
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
options = Options()
options.add_argument(
"user-agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'") # UA
s = Service('D:/software/msedgedriver.exe')
edge = webdriver.Edge(service=s, options=options)
其他浏览器
浏览器本身直接搜索下载,驱动driver可参考selenium官网的驱动下载列表:
https://www.selenium.dev/zh-cn/documentation/webdriver/troubleshooting/errors/driver_location/#download-the-driver。

2 运行报错
以下是运行Selenium可能遇到的问题:
RequestsDependencyWarning: urllib3 (1.26.9) or chardet (3.0.4) doesn‘t match a supported version
解决:更新requests:pip3 install --upgrade requests
打开了浏览器,但是没有显示网页 / Service连接失败
原因:浏览器驱动版本下载错误。
解决:请自行确定浏览器版本并重新下载驱动driver。
invalid argument: invalid locator (Session info: MicrosoftEdge=102.0.1245.44)
两种原因:
① 浏览器版本和驱动版本不一致(请自行确定浏览器版本并重新下载驱动driver);
② 代码打错了。比如edge.find_elements(by="//div"),正确的是edge.find_elements(by='xpath',value="//div")。
3 老代码报错
Selenium 4重构过,API 发生了一些变化。以下是常见报错:
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
原因:查询当前版本重构后的函数,是之前的 executable_path 被重构到了 Service 函数里。所以,新版的selenium不能继续用executable_path,而是应该写成Service。
DeprecationWarning 警告的类型错误的意思都是,该类型的警告大多属于版本已经更新,所使用的方法过时。
解决:webdriver.Edge(executable_path='/pathto/webdriver.exe', options=options) 改成 webdriver.Edge(service=Service('/pathto/webdriver.exe'), options=options),意思是去掉代码中的executable_path,用Service,如本文的第一节装里提供的示例代码那样写。
参考:selenium 报错 DeprecationWarning: executable_path has been deprecated, please pass in a Service object
AttributeError: ‘WebDriver’ object has no attribute ‘find_elements_by_xpath’
原因:新的driver类没有find_elements_by_xpath方法了:

解决:改成find_elements(by='xpath', value='查找路径')。
快速替换:find_elements_by_xpath( → find_elements(by='xpath',value=。
4 经典代码片段分享
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
def connectchrome():
"""
连接chrome浏览器,实现无痕浏览
"""
# driver_path = os.getcwd()+ "/chromedriver.exe"
# s = Service(driver_path)
s = Service("D:/software/chromedriver.exe") # 注意改成自己的driver路径
options = Options()
options.add_argument('log-level=3')
options.add_argument("--incognito")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option('excludeSwitches', ['enable-automation'])
prefs = {
'profile.default_content_setting_values': {
'images': 2,
}
}
options.add_experimental_option('prefs', prefs)
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument(
"user-agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'")
driver = webdriver.Chrome(service=s, options=options)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
driver.set_window_size(1280, 800)
driver.set_window_position(100, 100)
time.sleep(2)
return driver
本账号所有文章均为原创,欢迎转载,请注明文章出处:https://blog.csdn.net/qq_46106285/article/details/132405149。百度和各类采集站皆不可信,搜索请谨慎鉴别。技术类文章一般都有时效性,本人习惯不定期对自己的博文进行修正和更新,因此请访问出处以查看本文的最新版本。
【记录】Python3|Selenium4 极速上手入门(Windows)的更多相关文章
- 课程上线 -“新手入门 : Windows Phone 8.1 开发”
经过近1个月的准备和录制,“新手入门 : Windows Phone 8.1 开发”系列课程已经在Microsoft 虚拟学院上线,链接地址为:http://www.microsoftvirtuala ...
- 2.RABBITMQ 入门 - WINDOWS - 生产和消费消息 一个完整案例
关于安装和配置,见上一篇 1.RABBITMQ 入门 - WINDOWS - 获取,安装,配置 公司有需求,要求使用winform开发这个东西(消息中间件),另外还要求开发一个日志中间件,但是也是要求 ...
- 版本控制工具Git工具快速入门-Windows篇
版本控制工具Git工具快速入门-Windows篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 最近在学习Golang语言,之前的开发环境在linux上开发的,后来由于办公用的是w ...
- smarty半小时快速上手入门教程
http://www.jb51.net/article/56754.htm http://www.yiibai.com/smarty/smarty_functions.html http://www. ...
- Python3.7源码在windows(VS2015)下的编译和安装
Python3.7源码在windows(VS2015)下的编译和安装 下载官方源码,使用vs2015(WIN10SDK),最python3.7.0的源码进行编译,编译出不同的版本(release,de ...
- 华大单片机开发板HC32L13X上手入门
HC32L136开发板(如下图所示)分为板载调试模块(左半部分)和MCU开发电路(右半部分).二者中间通过邮票孔相连,如果将板子从中间掰开,板载调试模块就可以当一个CMSIS-DAP的仿真器来使用.此 ...
- 华大单片机开发板HC32F030上手入门
HC32F030开发板(如下图所示)分为板载调试模块(左半部分)和MCU开发电路(右半部分).二者中间通过邮票孔相连,如果将板子从中间掰开,板载调试模块就可以当一个CMSIS-DAP的仿真器来使用.此 ...
- 新手入门 : Windows Phone 8.1 开发 视频学习地址
本视频资源来自Microsoft Virtual Academy http://www.microsoftvirtualacademy.com/ 下面为视频下载地址! 新手入门 : Windows P ...
- Caffe+VS2015+python3的安装(基于windows)
在网上找了许多安装Caffe的教程 感觉全都是杂乱无章的 而且也没有详细的 只能自己当小白鼠来实验一次了 本次配置:CUDA 8.0+ CUDNN +VS 2015 +Python 3.5 + Ca ...
- firewalld 极速上手指南
从CentOS6迁移到7系列,变化有点多,其中防火墙就从iptables变成了默认Firewalld服务.firewalld网上资料很多,但没有说得太明白的.一番摸索后,总结了这篇文章,用于快速上手. ...
随机推荐
- 安全稳定地远程访问飞牛NAS
春节前从一个网友那里了解到一个新的NAS--飞牛. 起因是我们一个用户用我们的SD-WAN来远程访问飞牛NAS,市面上做NAS的很多,之所以单独体验这个产品主要是: 不需要购买硬件,这个是非常重要的, ...
- C# 如何解决文件写权限不可访问
原文链接 实际业务中,我们可能会遇到我们的安装包将程序安装在C盘Program Files目录下后,有些文件要修改或者新增会导致拒绝访问的异常.但是我们又不想把数据放临时文件夹AppData中,那么如 ...
- redis - [07] 数据类型
redis是一个开源(BSD许可)的,内存中的数据结构存储系统,可以用作数据库.缓存和消息中间件MQ.它支持多种类型的数据结构,如字符串(String).散列(Hash).列表(List).集合( ...
- VUE-CLI 创建VUE3项目
前言 第一篇当然是如何安装vue3 安装步骤 第一步安装vue-cli npm install -g @vue/cli // vue --version 第二步创建项目 vue create hell ...
- WebScoket-服务器客户端双向通信
WebScoket学习笔记 1. 消息推送常用方式介绍 轮询 浏览器以指定的时间间隔向服务器发出HTTP请求,服务器实时返回数据给浏览器. 长轮询 浏览器发出ajax请求,服务器端接收到请求后,会阻塞 ...
- [tldr]通过指令获取github仓库的单个文件的内容
针对一个公开的github仓库,有些时候不需要clone整个仓库的内容,只需要对应的几个文件.但是直接通过网页点击下载文件很麻烦,在服务器上也不好这样操作. 因此,如何使用curl或者wget指令快速 ...
- 题解:CF2077B Finding OR Sum
本文发布于博客园和洛谷,若您在其他平台阅读到此文,请前往博客园获得更好的阅读体验. 跳转链接:https://www.cnblogs.com/TianTianChaoFangDe/p/18771334 ...
- mysql 表的创建,修改,删除
查看数据库所有表 show tables 创建 create table 表名 ( 列名 类型 约束条件 ... ) 类型有整形: tinyint(1B) ,smallint(2B),mediumin ...
- k8s node节点报错 dial tcp 127.0.0.1:8080: connect: connection refused
前言 在搭建好 kubernetes 环境后,master 节点拥有 control-plane 权限,可以正常使用 kubectl. 但其他 node 节点无法使用 kubectl 命令,即使同步过 ...
- MySQL查询当前连接数的语句
1. 查看当前总连接数 SHOW STATUS LIKE 'Threads_connected'; 返回当前建立的连接总数 2. 查看最大连接数配置 SHOW VARIABLES LIKE 'max_ ...