> 如果你只使用 s 模式(即requests),可跳过本节。 使用 selenium 前,必须配置 chrome.exe 和 chromedriver.exe 的路径,并确保它们版本匹配。 配置路径有四种方法: - 使用 easy_set 工具的 get_match_driver() 方法(推荐) - 将路径写入本库的 ini 文件 - 将两个路径写入系统变量 - 在代码中填写路径 ## 使用 get_match_driver() 方法 若你选择第一种方式,请在第一次使用前,运行以下代码,程序会自动检测电脑安装的 chrome 版本,下载对应 driver,并记录到 ini 文件。 ```python from DrissionPage.easy_set import get_match_driver get_match_driver() ``` 输出: ``` ini文件中chrome.exe路径 D:\Google Chrome\Chrome\chrome.exe version 75.0.3770.100 chromedriver_win32.zip Downloading to: D:\python\projects\DrissionPage\DrissionPage 100% Success. 解压路径 D:\python\projects\chromedriver.exe 正在检测可用性... 版本匹配,可正常使用。 ``` 然后就可以开始使用了。 若你想使用指定的 chrome.exe(绿色版),及指定 ini 文件和 chromedriver.exe 的保存路径,可以这样写: ```python get_match_driver(ini_path='ini文件路径', save_path='保存路径', chrome_path='chrome路径') ``` Tips:当指定 chrome_path 时,检测成功后程序会把这个路径写进 ini 文件。 ## 使用 set_paths() 方法 若上一种方法失败,可自行下载 chromedriver.exe,然后运行以下代码,把路径记录到 ini 文件中。 ```python from DrissionPage.easy_set import set_paths driver_path = 'D:\\chrome\\chromedriver.exe' # 你的 chromedriver.exe 路径,若不填写会在系统变量中查找 chrome_path = 'D:\\chrome\\chrome.exe' # 你的 chrome.exe 路径,若不填写会在系统变量中查找 set_paths(driver_path, chrome_path) ``` 该方法还会检查 chrome 和 chromedriver 版本是否匹配,显示: ``` 正在检测可用性... 版本匹配,可正常使用。 ``` 或 ``` 出现异常: Message: session not created: Chrome version must be between 70 and 73 (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.19631 x86_64) 可执行easy_set.get_match_driver()自动下载匹配的版本。 或自行从以下网址下载:https://chromedriver.chromium.org/downloads ``` 检查通过后,即可正常使用 driver 模式。 除了上述两个路径,该方法还可以设置以下路径: ```python debugger_address # 调试浏览器地址,如:127.0.0.1:9222 download_path # 下载文件路径 tmp_path # 临时文件夹路径 user_data_path # 用户数据路径 cache_path # 缓存路径 ``` Tips: - 不同项目可能须要不同版本的 chrome 和 chromedriver,你还可保存多个 ini 文件,按须使用。 - 推荐使用绿色版 chrome,并手动设置路径,避免浏览器升级造成与 chromedriver 版本不匹配。 - 调试项目时推荐设置 debugger_address,使用手动打开的浏览器,再用程序接管,好处多多。 ## 其它方法 若你不想使用 ini 文件(如要打包项目时),可在系统路径写入以上两个路径,或在程序中填写。后者的使用方法见下一节。