Selenium環(huán)境搭建

1. 安裝Python
2. 安裝基于Python的Selenium包
  1. 新建一個(gè)專門放置腳本的目錄:D:/Python/Scripts
  2. 輸入命令pip install selenium
    這里有可能報(bào)錯(cuò),加--user即可:pip install selenium --user
  3. pip show selenium查看版本看是否安裝成功
3. 運(yùn)行自動(dòng)化腳本
  1. 在本地建一個(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()
  1. 在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)了

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容