Python百度输入关键字进行搜索脚本运行报错
在一本书上看到的例子,使用Firefox浏览器,打开百度网页,输入关键字进行搜索。照着例子把代码敲完,运行的时候报错。代码如下:
#coding=utf-8from selenium import webdriver driver = webdriver.Firefox()driver.get("http://www.baidu.com") driver.find_element_by_id("kw").send_keys("Selenium2")driver.find_element_by_id("su").click()driver.quit()
报错信息如下:
>>> Traceback (most recent call last):File"C:\Python34\lib\site-packages\selenium\webdriver\common\service.py",line 74, in start stdout=self.log_file, stderr=self.log_file)File"C:\Python34\lib\subprocess.py", line 859, in __init__ restore_signals, start_new_session)File"C:\Python34\lib\subprocess.py", line 1112, in _execute_child startupinfo)FileNotFoundError: 系统找不到指定的文件。 During handling of the above exception, anotherexception occurred: Traceback (most recent call last):File"D:/code/baidu.py", line 4, in <module> driver =webdriver.Firefox()File"C:\Python34\lib\site-packages\selenium\webdriver\firefox\webdriver.py",line 140, in __init__ self.service.start()File"C:\Python34\lib\site-packages\selenium\webdriver\common\service.py",line 81, in start os.path.basename(self.path), self.start_error_message)selenium.common.exceptions.WebDriverException:Message: 'geckodriver' executable needs to be in PATH.
我是电脑是windowsXP,装的有Firefox浏览器。python以及selenium信息如下:
C:\Python34>pythonPython 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015,22:43:06) on win32Type "help", "copyright","credits" or "license" for more information.>>>C:\Python34>pip show seleniumName: seleniumVersion: 3.0.2Summary: Python bindings for SeleniumHome-page: https://github.com/SeleniumHQ/selenium/Author: UNKNOWNAuthor-email: UNKNOWNLicense: UNKNOWNLocation: c:\python34\lib\site-packagesRequires:C:\Python34>pythonPython 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015,22:43:06) on win32Type "help", "copyright","credits" or "license" for more information.>>>
请教,这是什么原因呢?为什么还是报找不到文件呢,系统装的有Firefox浏览器。
selenium.common.exceptions.WebDriverException:Message: 'geckodriver' executable needs to be in PATH.
错误信息提示你,geckodriver不在路径上
一是你没有geckodriver,或是你的geckodriver不在指定的路径上。
geckodriver是selenium3.0.x版本开始有点,用于驱动火狐浏览器。
geckodriver的下载地址为:https://github.com/mozilla/geckodriver/releases
缺少驱动文件吧你单独试下启动浏览器估计单独浏览器都启动不了 下载这个geckodriver,把geckodriver.exe解压出来,把这个文件的路径加入path中 正解在此:
1.确定你的firefox版本,不能太新。稳定点的找48以前的版本,切记不要用最新的
2.下载geckodriver,然后放到你的火狐浏览器的根目录下,就ok 正解在此:
1.火狐版本稳定在48,不要用最新版本
2.geckodriver.exe下载后放到火狐的根目录下
页:
[1]