techiall / Blog

🍋 [My Blog] See discussions
https://github.com/techiall/Blog/discussions
MIT License
8 stars 1 forks source link

selenium python 环境搭建 #24

Open techiall opened 6 years ago

techiall commented 6 years ago

环境

Python 安装

selenium 安装

  1. pip 安装 selenium: pip install selenium (如果用了 anaconda,会提示已经存在

  2. 根据自己的使用的浏览器选择对应的驱动(我用的是 Chrome

  3. 打开 Chrome — 设置 — 关于Chrome,找到 Chrome 的版本号

    1. 版本 69.0.3497.23(正式版本) (64 位),之前用的是 70.+ 版本,驱动没支持 70.0 版本以上只能回退回来。
  4. http://chromedriver.chromium.org/downloads Chrome 版本对应的驱动号

    ChromeDriver 2.40
    
    Supports Chrome v66-68
  5. https://chromedriver.storage.googleapis.com/index.html 找到自己电脑对应 Chrome 版本驱动和平台下载到本地,解压。

  6. 将 Chromedriver 的目录添加到 环境变量 。如果不想配置环境变量的话写代码就得添加 Chromedriver 的路径。

Chrome 安装 Katalon Recorder 插件

测试

from selenium.webdriver import Chrome,ChromeOptions

driver_path = r"D://chromedriver.exe"  # Chromedriver 路径
Options = ChromeOptions()

# 浏览器路径,如果有 Chrome 安装的话, 会自动查找,绿色版会有问题
# 如果有多个版本的 Chrome,可以指定对应的版本
Options.binary_location = r"Chrome.exe" # 浏览器路径
driver = Chrome(chrome_options=Options, executable_path = driver_path, port=9515) 

driver.get( "https://www.baidu.com")   #打开百度

GreenChrome

参考链接