Python运行selenium报错:selenium.common.exceptions.WebDriverException: Message: 'MicrosoftWebDriver.exe'

最近在看视频,里面的老师用到了selenium去打开浏览器并爬取数据。

我尝试了一下,却报错了。

Traceback (most recent call last):
  File "C:\Users\Sartin\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
    stdin=PIPE)
  File "C:\Users\Sartin\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\Sartin\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/TaiDiProject/Python爬虫助力疫情数据追踪/Pycharm-code/网络爬虫实战/常规动态网页爬取/Selenium打开浏览器.py", line 7, in <module>
    driver = webdriver.Edge()
  File "C:\Users\Sartin\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\edge\webdriver.py", line 56, in __init__
    self.edge_service.start()
  File "C:\Users\Sartin\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'MicrosoftWebDriver.exe' executable needs to be in PATH. Please download from http://go.microsoft.com/fwlink/?LinkId=619687

查了一下,原来是没有把下载好的msedgedriver.exe放到浏览器目录里,而且代码也要说明具体位置。

首先我用的是Microsoft Edge,需要的Drive到这下载

一般解压后就OK了,把里面的msedgedriver.exe放到python.exe目录下所在浏览器目录下

比如我的浏览器目录:C:\Program Files (x86)\Microsoft\Edge\Application

在这里插入图片描述
接着是代码部分,你要说明msedgedriver.exe所在路径(C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe)

from selenium import webdriver

# msedgedriver.exe下载后放到浏览器的目录下
driver = webdriver.Edge("C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe")
driver.get("https://www.ptpress.com.cn/")   # 你要进入的浏览器

这样就成功了!

在这里插入图片描述

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值