目录
一、问题描述
二、问题分析
1、异常一:
2、异常二:
3、分析
三、解决方法
1、geckodriver的下载
2、检查代码
四、验证
前言
Selenium的脚本可以控制浏览器进行操作,可以实现多个浏览器的调用,包括
IE
(7
、
8
、
9
、
10
、
11
)、
Firefox
、
Safari
、
Google Chrome
、
Opera
等。常用的是
Firefox
,因此下面的讲解也以
Firefox
为例
。
大家对应自己的情况进行去解决。
|
一、问题描述
运行代码时程序报错,出现了以下两个异常,即无法使用Selenium打开浏览器和一个网页:
F:\PyCharm-community\2020.3.5版本\2020.3\WebScraping\Scripts\python.exe F:/PyCharm-community/pythonProject/WebScraping/11.1.1.py
Traceback (most recent call last):
File "F:\PyCharm-community\2020.3.5版本\2020.3\WebScraping\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\MAIBENBEN\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 969, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\MAIBENBEN\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1438, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\PyCharm-community\pythonProject\WebScraping\11.1.1.py", line 6, in <module>
driver=webdriver.Firefox()
File "F:\PyCharm-community\2020.3.5版本\2020.3\WebScraping\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 172, in __init__
self.service.start()
File "F:\PyCharm-community\2020.3.5版本\2020.3\WebScraping\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Process finished with exit code 1
文章来源地址https://uudwc.com/A/e10Ro
文章来源:https://uudwc.com/A/e10Ro
二、问题分析
1、异常一:
翻译:
【in_execute_child
hp,ht,pid,tid=_winapi.CreateProcess(可执行文件,参数,
FileNotFoundError:[WinError 2]系统找不到指定的文件。】
2、异常二:
翻译:
【启动中
引发WebDriverException(
selenium.common.exeptions.WebDriverException:消息:“geckodriver”可执行文件需要在PATH中。】
3、分析
两个异常都出现了“executable”,而executable的翻译是可执行文件。经过分析可得,是geckodriver.exe的问题。
首先你的电脑里应该得下载了驱动程序geckodriver.exe;其次看代码中有没有写明geckodriver.exe的地址,如果有,则可能是你地址没有写完整,比如缺了“geckodriver.exe”之类的;如果没有,则加上可执行文件(geckodriver.exe)的地址就可以了。
三、解决方法
1、geckodriver的下载
【链接】:https://github.com/mozilla/geckodriver/releases
下载相应操作系统的geckodriver,这是一个压缩文件,解压后可以放在桌面,如:C:\Users\santostang\Desktop\geckodriver.exe。
2、检查代码
【我的geckodriver.exe的地址】
F:\1Senior4\geckodriver---selenium\geckodriver.exe
【我的部分代码】
可见,没有写geckodriver.exe的地址。需要在第6行代码里添加上。
【措施】
将
driver = webdriver.Firefox()
修改为
driver=webdriver.Firefox(executable_path = r'F:\1Senior4\geckodriver---selenium\geckodriver.exe')
四、验证
运行代码,成功打开了Firefox浏览器和一个网页
本文至此完,感谢阅读