selenium的安裝、報錯和解決


  selenium是的作用是模擬點擊瀏覽器上的按鈕,配合一個無頭瀏覽器就可以快速解決一些前端需要加解密的功能。

第一步pip install

selenium安裝的第一步就是用pip把模塊下載回來。

pip install selenium

具體報錯信息如下:

File "/usr/local/lib/python2.7/dist-packages/selenium-3.0.0b2-py2.7.egg/selenium/webdriver/firefox/webdriver.py", line 65, in __init__
self.service.start()
File "/usr/local/lib/python2.7/dist-packages/selenium-3.0.0b2-py2.7.egg/selenium/webdriver/common/service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x7f753ad53390>> ignored

#  發現報錯信息包含: Message: 'geckodriver' executable needs to be in PATH

報錯原因 :
  是因為缺少依賴包geckodriver,所以在安裝selenium之前,先把geckodriver安裝上就能解決了。

不同平台下有不同解決方案:
Windows系統

  1. 下載geckodriver.exe:

    下載地址:https://github.com/mozilla/geckodriver/releases

    請根據系統版本選擇下載;(如Windows 64位系統)

  2. 下載解壓后將getckodriver.exe復制到Firefox的安裝目錄下,如(C:\Program Files\Mozilla Firefox),並在環境變量Path中添加路徑:C:\Program Files\Mozilla Firefox;

  3. 重啟cmd后,再次運行代碼即可

ubuntu系統

  1. 下載 geckodriverckod 地址:https://github.com/mozilla/geckodriver/releases
  2. 解壓后將geckodriverckod 存放至 /usr/local/bin/ 路徑下即可

Mac OS系統:
  如果你已經安裝好了brew,那么輸入如下兩條命令即可

brew install geckodriver
brew link geckodriver

  Mac OS檢驗geckodriver是否安裝成功,

# shell窗口下輸入命令
geckodriver

# 什么都不輸出,也不退出,說明安裝成功
# 注意:windows 系統需要重啟一個新cmd窗口在輸命令

最后使用pip list查看一下已安裝的庫:

發現已經包含selenium,說明安裝成功。


第二步Python運行Demo

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')

報錯:

Traceback (most recent call last):
  File "D:\programfiles\Anaconda3\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
    stdin=PIPE)
  File "D:\programfiles\Anaconda3\lib\subprocess.py", line 756, in __init__
    restore_signals, start_new_session)
  File "D:\programfiles\Anaconda3\lib\subprocess.py", line 1155, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系統找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/code/commany/python/APPScan/main.py", line 7, in <module>
    browser = webdriver.Chrome()
  File "D:\programfiles\Anaconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "D:\programfiles\Anaconda3\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: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Process finished with exit code 1

沒有安裝chromedriver導致的,參考自己chrome瀏覽版本對應的chromedriver版本:
https://my.oschina.net/u/3367404/blog/3011400

chromedriver下載地址:
https://chromedriver.storage.googleapis.com/index.html

下載完成后,將ChromeDriver的可執行文件配置到環境變量下。

如果使用Windows,建議直接將chromedriver.exe文件拖到Python的Scripts目錄下。

再次運行腳本,發現會新彈出一個新chrome。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM