今天在學習python中的selenium時出現了一些錯誤,後來解決了,在這裏與大家分享解決方法。
代碼如下:
from selenium import webdriver
brower = webdriver.Firefox()
brower.get('https://www.taobao.com')
print(brower.page_source)
brower.close()
最初的運行結果是:
Traceback (most recent call last):
File "/home/eric/.local/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "/usr/lib/python3.5/subprocess.py", line 947, in init
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/eric/PycharmProjects/untitled12/12.py", line 2, in
brower = webdriver.Firefox()
File "/home/eric/.local/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 144, in init
self.service.start()
File "/home/eric/.local/lib/python3.5/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.
Process finished with exit code 1
通過求助,了解到是瀏覽器缺少驅動造成的問題。也就是缺少geckodriver。
1.於是先下載驅動(https://github.com/mozilla/geckodriver/releases)(由於我是linux系統,我下載的是geckodriver-v0.18-linux64.tar.gz
2.下載完成後,打開終端
sudo tar zxvf geckodriver-v0.18.0-linux64.tar.gz
sudo mv geckodriver /usr/bin/ #移动到指定路径
cd /usr/bin/ #进入路径
sudo chmod +x geckodriver #除去权限
3.重新啓動程序,正常運行,結果由於太長,在此就不粘貼類。
最後感謝爲我提供幫助的人。