selenium加载驱动的两种方式

发布时间 2024-01-12 15:47:13作者: Alicute

以chrome浏览器举例

第一种

  1. 查看本地chrome浏览器版本:右上角-设置-关于chrome,我的版本是:120.0.6099.200

  2. 去对应驱动下载网站下载

    1. 常用网站(更新到114):https://registry.npmmirror.com/binary.html?path=chromedriver
    2. 新版驱动网站:https://googlechromelabs.github.io/chrome-for-testing/
  3. 下载好对应的网站后,将其放到与chrome安装路径一致的地方,然后将该目录添加到:系统变量-Path 中

  4. 然后就是去Pycharm中引入对应的包和创建函数对象

    from selenium import webdriver
    driver = webdriver.Chrome()
    driver.quit()
    

第二种

  1. # 打开pycharm,安装webdriver-manager, pip install webdriver-manager
    # 通过这个模块管理各类浏览器驱动,程序运行后会自动安装对应浏览器的驱动,安装目录在:%USERPROFILE%\.wdm\drivers\chromedriver
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service as ChromeService
    from webdriver_manager.chrome import ChromeDriverManager
    service = ChromeService(executable_path=ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service)
    driver.quit()
    

附带各类驱动下载地址

GeckoDriver(Firefox):https://github.com/mozilla/geckodriver/releases
ChromeDriver(Chrome):https://sites.google.com/a/chromium.org/chromedriver/home
IEDriverServer(IE):http://selenium-release.storage.googleapis.com/index.html
OperaDriver(Opera):https://github.com/operasoftware/operachromiumdriver/releases
MicrosoftWebDriver(Edge):https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver