firefox 47以上版本,需要下載第三方driver,即geckodriver

riyue2017發表於2017-06-05
from selenium import webdriver
driver=webdriver.Firefox()
driver.get("http://www.baidu.com")
text=driver.find_element_by_id("cp").text

print(text)

執行結果:

Traceback (most recent call last):
  File "D:/Python27/baidu.py", line 2, in <module>
    driver=webdriver.Firefox()
  File "D:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 142, in __init__
    self.service.start()
  File "D:\Python27\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
    os.path.basename(self.path), self.start_error_message)
WebDriverException: Message: 'geckodriver' executable needs to be in PATH.


解決方法:

去搜尋下載geckodriver.exe 放到C:\Python27\Scripts目錄下


方法:(一)1. selenium 3.x開始,webdriver/firefox/webdriver.py的__init__中,executable_path="geckodriver";而
2.x是executable_path="wires"2. firefox 47以上版本,需要下載第三方driver,即geckodriver;
在http://docs.seleniumhq.org/download/的Third Party Drivers, Bindings, and Plugins下面找到Mozilla GeckoDriver,
下載到任意電腦任意目錄,解壓後將該路徑加入到PC的path(針對windows)即可。
(二)下載 geckodriverckod   地址: mozilla/geckodriver解壓後將geckodriverckod 存放至 /usr/local/bin/ 路徑下即可
sudo mv ~/Downloads/geckodriver /usr/local/bin/
(三)解決的程式碼:
*****from selenium import webdriver
*****import time
******dr = webdriver.Firefox(executable_path = '/Users/jinwenxin/desktop/pythonPractice/geckodriver')
*****time.sleep(5)
******print 'Browser will close.'
****dr.quit()
****print 'Browser is close'教大家一個方法,我用了在python下執行了help(webdriver.Firefox) 回車,這樣就知道了傳什麼引數。

相關文章