Python3中selenium的瀏覽器運行環境


這里只說 Firefox 和 Chrome 瀏覽器

一、已安裝Python3和PyCharm Community環境

二、pip 和 selenium 的安裝

1、pip 有的在 Python 中是自帶的,cmd 窗口輸入:pip list 查看;

  

  

  有可能會提示 pip 版本不匹配,就按提示更新。

2、cmd 窗口 輸入:pip install selenium,安裝selenium :

這樣 pip 和 selenium 安裝好了。

三、驅動程序和運行

1、Firefox瀏覽器:

  版本一定要大於48,在:https://github.com/mozilla/geckodriver/releases/,下載 geckodriver.exe 壓縮包,解壓后放在 Python 安裝目錄下,如:D:\Python37;

2、Chrome瀏覽器:

  1)打開瀏覽器查看版本:chrome://version/

  2)到:http://chromedriver.storage.googleapis.com/index.html? 下載合適的 chromedriver.exe,64位的向下兼容,可以下載32的。解壓后放在 Python 安裝目錄下。 

  3)運行,以chrome瀏覽器為例:

  沒有瀏覽器驅動程序時會報錯:selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH。

  百度這一行錯誤提示可以找到解決方法,就是上面所說的,正確的下載了驅動程序后可以正常運行。

附: 

from selenium import webdriver
import time

chrome_driver = "D:\Python37\chromedriver.exe"
driver = webdriver.Chrome(executable_path= chrome_driver)
time.sleep(5.5)

driver.maximize_window()
driver.get('http://www.baidu.com/')
driver.find_element_by_id('kw').send_keys('selenium')
#driver.refresh()
driver.quit()

  注意:電腦的瀏覽器會經常更新版本,過段時間運行腳本,會報:selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 75。所以需要重新更新合適的驅動程序 chromedriver.exe。

 

謝謝查看,筆記持續完善!

2019-10-19更新


免責聲明!

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



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