zoukankan      html  css  js  c++  java
  • python一行命令安装chromedriver

    前言

    在selenium web-UI自动化测试之前我们需要配置环境变量,安装包插件等,其中最重要的就是chromedriver浏览器。

    配置好了chromedriver我们才能成功的打开浏览器,但是安装过程却是比较麻烦的,比如我们需要先下载chromedriver插件,然后配置环境变量,什么都配置好了,才能使用selenium打开浏览器。

    有没有更好更简便的方法呢,还真有这样一个简便的方法。

    Github上面有人就开源了一个chromedriver-py库。

    安装

    首先呢,删除本机的chromedriver

    rm /usr/local/bin/chromedriver
    

    然后呢,我们需要安装这个库:

    • MacOS和Linux平台
    python3 -m pip install chromedriver-py
    
    • win平台
    pip install chromedriver-py
    

    安装过程:

    $ python3 -m pip install chromedriver-py
    Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
    Collecting chromedriver-py
      Downloading http://mirrors.aliyun.com/pypi/packages/6c/7e/96661812736a0d896f445752c3ebf31baf0d86d0dddeae50a0048d1fe42b/chromedriver_py-87.0.4280.88-py3-none-any.whl (19.6 MB)
         |████████████████████████████████| 19.6 MB 5.9 MB/s
    Installing collected packages: chromedriver-py
    Successfully installed chromedriver-py-87.0.4280.88
    WARNING: You are using pip version 20.1.1; however, version 20.3.1 is available.
    You should consider upgrading via the '/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install --upgrade pip' command.
    

    编写脚本测试一下

    首先我们需要导入两个模块

    import time
    from selenium import webdriver
    from chromedriver_py import binary_path
    

    然后编写启动浏览器

    driver = webdriver.Chrome(executable_path=binary_path)
    

    这里在driver初始化的时候一定要指定executable_path参数为chromedriver_py模块的binary_path变量。

    driver.get('https://www.baidu.com')
    time.sleep(3)
    driver.quit()
    

    然后执行,成功启动浏览器,打开百度,停留3秒关闭。

    探究

    我们一起找找chromedriver在哪里下载着,首先我们是用pip命令安装的,所以我们进入site-package目录下看看。

    在里面找到了chromedriver_py的文件夹。我们打开:

    img

    会发现,它会给你自动下载了三个平台的chromedriver的驱动包。然后python这样运行的时候就会被执行了。

    这个真的很方便也很实用。最后推荐大家使用这种方式。

  • 相关阅读:
    POJ 1141 括号匹配 DP
    881. Boats to Save People
    870. Advantage Shuffle
    874. Walking Robot Simulation
    文件操作
    861. Score After Flipping Matrix
    860. Lemonade Change
    842. Split Array into Fibonacci Sequence
    765. Couples Holding Hands
    763. Partition Labels
  • 原文地址:https://www.cnblogs.com/wxhou/p/chromedriver-py.html
Copyright © 2011-2022 走看看