1. 安裝Python
2. 安裝基于Python的Selenium包
- 新建一個(gè)專門放置腳本的目錄:D:/Python/Scripts
- 輸入命令
pip install selenium
這里有可能報(bào)錯(cuò),加--user即可:pip install selenium --user -
pip show selenium查看版本看是否安裝成功
3. 運(yùn)行自動(dòng)化腳本
- 在本地建一個(gè)test.py的腳本文件,里面寫(xiě)個(gè)簡(jiǎn)單的自動(dòng)化腳本,如下:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("selenium2")
driver.find_element_by_id("su").click()
# driver.quit()
- 在cmd下運(yùn)行該腳本
python test.py
發(fā)現(xiàn)會(huì)報(bào)錯(cuò),報(bào)錯(cuò)信息如下:
Traceback (most recent call last):
File "C:\Users\Pillar_it3\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Program Files\Python37\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Program Files\Python37\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系統(tǒng)找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 3, in <module>
driver = webdriver.Chrome()
File "C:\Users\Pillar_it3\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\Pillar_it3\AppData\Roaming\Python\Python37\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
原因是缺少chrome的一個(gè)驅(qū)動(dòng)文件。
4. 下載并安裝瀏覽器驅(qū)動(dòng)
chrome下載地址:
https://sites.google.com/a/chromium.org/chromedriver/home
Firefox的geckodriver下載地址:
https://github.com/mozilla/geckodriver/releases
查看本地的chrome版本,然后下載對(duì)應(yīng)的驅(qū)動(dòng)。
下載完成后本地解壓,把exe文件拷貝到python的安裝目錄下:

python安裝目錄.png
我本地至此就可以通過(guò)python test.py跑起來(lái)了