Chromedriver放项目里就行!Selenium 3 和 4 指定路径方法对比 + 兼容写法

我们在做自动化测试的时候经常会遇到的一个问题:浏览器驱动chromedriver应该放在哪里

小编在上篇文章中,给大家推荐过不同电脑设置chromedriver最佳路径

Python自动化测试入门必备:ChromeDriver是个啥?咋下载配置?

但有小伙伴后台私信问:可以把chromedriver放在项目中,然后在代码中写死指定路径吗

这样做的目的是将chromedriver和代码一起打包,之后拷贝到任意一台电脑上,都无需再次重新配置chromedriver路径

非常nice的需求,小编以前咋没想到过呢?接下来就是chromedriver指定路径的代码实现 ,但需要注意在Selenium3和Selenium4的写法是有区别的,小伙伴们一定要查看好自己的Selenium版本再进行编写,或者小编最后也会给大家提供了一个兼容Selenium3和4两种版本的代码实现方法

话不多说,我们直接上代码!

Selenium 3

此方法仅适用于Selenium3

实现方法:首先将chromedriver路径传递给executable_path变量,之后在webdriver.Chrome()中传入executable_path参数即可

python 复制代码
from selenium import webdriver
​
class GetDriver:
    driver = None
​
    def get_driver(self, url):
        # 使用指定chromedriver路径
        driver_path="/Users/youth/Downloads/code/chromedriver"
        self.driver = webdriver.Chrome(executable_path=driver_path)
​
        # 打开url
        self.driver.get(url)
        # 返回driver
        return self.driver

Selenium 4

此方法同样适用于Selenium3和Selenium4

注意:上方导包也新增了Service类的导入

实现方法:首先将chromedriver实际路径传递给Service类,之后在webdriver.Chrome()中传入service参数

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
​
class GetDriver:
    driver = None
​
    def get_driver(self, url):
        # 指定chromedriver路径
        driver_path="/Users/youth/Downloads/code/chromedriver"
        # 使用Service对象
        service = Service(executable_path=driver_path)
        self.driver = webdriver.Chrome(service=service)
             
        # 打开url为
        self.driver.get(url)
        # 返回driver
        return self.driver

对比

版本 Selenium 3 Selenium 4
指定路径方式 webdriver.Chrome(executable_path='path') webdriver.Chrome(service=Service('path'))
Service对象 可选使用 推荐使用
executable_path参数 直接支持 已弃用,需通过Service对象
代码简洁性 较简洁 稍显冗长但更规范

兼容性写法

如果想让代码兼容Selenium 3和4两个版本,可以这样写:

python 复制代码
from selenium import webdriver
from tools.get_log import GetLog
​
log = GetLog.get_logger()
​
class GetDriver:
    driver = None
    
    def get_driver(self, url, driver_path=None):
        log.info("判断是否为空")
        if self.driver is None:
            log.info("为空,启动浏览器驱动")
            
            try:
                # 尝试Selenium 4的写法
                from selenium.webdriver.chrome.service import Service
                if driver_path:
                    log.info(f"使用Selenium 4方式指定chromedriver路径: {driver_path}")
                    service = Service(executable_path=driver_path)
                    self.driver = webdriver.Chrome(service=service)
                else:
                    log.info("使用系统PATH中的chromedriver")
                    self.driver = webdriver.Chrome()
            except ImportError:
                # 回退到Selenium 3的写法
                if driver_path:
                    log.info(f"使用Selenium 3方式指定chromedriver路径: {driver_path}")
                    self.driver = webdriver.Chrome(executable_path=driver_path)
                else:
                    log.info("使用系统PATH中的chromedriver")
                    self.driver = webdriver.Chrome()
            
            log.info("最大化")
            self.driver.maximize_window()
            log.info(f"打开url为: {url}")
            self.driver.get(url)
            log.info("返回driver")
            return self.driver
​
    def quit_driver(self):
        log.info("判断是否为空")
        if self.driver:
            log.info("不为空,关闭driver")
            self.driver.quit()
            log.info("置空数据")
            self.driver = None

既然Selenium官方修改了指定路径的写法,那么小编还是更推荐使用Selenium 4 的方法,毕竟它版本更新且有更好的维护,只是相比Selenium3版本麻烦一些。当然最保险的做法就是直接使用兼容版本代码,可以完美的兼容Selenium3和4两个版本(但会造成代码的复杂和冗余)

好啦!如果你觉得这篇文章对你有帮助,请给我点个赞吧!关于配置chromedriver以及selenium的一些其他问题也可以评论、私信问我,感谢大家的观看!

相关推荐
HyperAI超神经20 小时前
【vLLM 学习】Prithvi Geospatial Mae
人工智能·python·深度学习·学习·大语言模型·gpu·vllm
逻极20 小时前
Python MySQL防SQL注入实战:从字符串拼接的坑到参数化查询的救赎
python·mysql·安全·sql注入
赫凯20 小时前
【强化学习】第一章 强化学习初探
人工智能·python·强化学习
Amewin20 小时前
window 11 安装pyenv-win管理不同的版本的python
开发语言·python
lionliu051920 小时前
WebAssembly (Wasm)
java·开发语言·wasm
咸鱼加辣20 小时前
【java面试题】springboot的生命周期
java·开发语言·spring boot
weixin_4624462320 小时前
用 Go 快速搭建一个 Coze (扣子)API 流式回复模拟接口(Mock Server)
开发语言·golang·状态模式
小鸡吃米…20 小时前
Python编程语言面试问题二
开发语言·python·面试
谁动了我的代码?21 小时前
QT<34> 利用线程池处理耗时任务以及回调函数的使用
开发语言·qt
柒.梧.21 小时前
数据结构:二叉排序树构建与遍历的解析与代码实现
java·开发语言·数据结构