python环境selenium的安装和配置以及常见错误的解决方法 |
您所在的位置:网站首页 › pip安装成功显示什么 › python环境selenium的安装和配置以及常见错误的解决方法 |
1. 安装方法
selenium包的安装有以下几个方式: 通过命令行安装,语法为pip install selenium[==版本号],其中版本号是选填的,比如安装版本号为2.48.0的selenium,则命令为pip install selenium==2.48.0,如果是要安装最新版本的则直接输入pip install selenium即可。通过pycharm安装,具体步骤为:菜单File–>setting–>Project 当前工程名称–>Project Interpreter–>点击+号–>搜索(selenium)–>勾选 Specify version选项选择版本–>Install Package,具体的步骤如下图:![]() ![]() ![]() 在windows搜索框输入cmd,打开命令行后输入pip install selenium,回车,如果出现下图信息,表示安装成功。 在windows搜索框搜索cmd打开cmd命令行后再运行selenium的安装命令或pip的更新命令python -m pip install --upgrade pip,一般在cmd命令行打开后的默认路径运行命令,如果在其他路径下打开cmd运行安装或者更新命令时,会出现一些意想不到的错误。 4. 安装浏览器驱动 下载浏览器驱动压缩包,各个浏览器驱动的下载地址: Chrome内核的驱动安装包下载地址http://chromedriver.storage.googleapis.com/index.html 火狐的驱动下载地址为:https://github.com/mozilla/geckodriver/releases/ IE浏览器驱动的下载地址:https://www.nuget.org/packages/Selenium.WebDriver.IEDriver/这里以360极速浏览器为例,首先,查看浏览器的内核版本信息,如下图:![]() ![]() ![]() ![]() 没有将chromedriver.exe放到与python.exe的同级目录,并且没有将其加入环境变量,有以下两个解决方法: 按照上述的方法配置chromedriver.exe的相关信息手动指定驱动chromedirver.exe的位置,如下: from selenium import webdriver browser = webdriver.Chrome(executable_path=r"绝对路径\chromedirver.exe") >报selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary错误检查浏览器是否在默认位置安装,如果在浏览器安装的过程中改变了浏览器的安装位置,则需要在代码中指定浏览器的安装位置,如下: # 方法一 from selenium import webdriver options = webdriver.ChromeOptions() options.binary_location = r"D:\360\360Chrome\Chrome\Application\360chrome.exe" browser = webdriver.Chrome(chrome_options=options)切记,此错误不能用如下方法指定浏览器位置: # 方法二 from selenium import webdriver browser = webdriver.Chrome(r"D:\360\360Chrome\Chrome\Application\360chrome.exe")如果用方法二指定浏览器位置,则会报selenium.common.exceptions.webDriverException: Message: Service D:\360\360Chrome\Chrome\Application\360chrome.exe unexpectedly exited. Status code was: 0错误。此时,浏览器只能被打开,但selenium无法操控浏览器。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |