1.更換Python版本
打開pycharm,點擊 file——setting——project項目名——project Interpreter,點擊右側的設置,如下圖
選擇新Python版本的安裝路徑。
如果location提示“environment location directory is not empty”,導致無法保存。則將location路徑的文件夾刪掉,然后再重新設置就可以保存了。
2.添加selenium包
沒有安裝selenium包時,程序中importselenium包報錯:
no module named 'selenium'
查看Python3安裝目錄 ..\python37\Script\目錄下是否存在pip.exe,並確保該目錄已添加到“環境變量”的“path”下面。
打開windows命令提示符,輸入“pip”命令,確保該命令可以執行成功。
然后按照安裝selenium包。
輸入命令:pip install selenium
安裝成功后,輸入:from selenium import selenium
不報錯,表示selenium包安裝成功
然后在重新配置project interpret,步驟與1相同。
3.添加Chromedriver包
常見報錯:
driver = webdriver.Chorme()
AttributeError: module 'selenium.webdriver' has no attribute 'Chorme'
查找Chrome版本對應的Chromedriver版本:https://chromedriver.storage.googleapis.com/index.html
下載相應的版本后,將 Chromedriver.exe 放到Chrome安裝目錄下
配置環境變量:將Chromedriver.exe的路徑加到環境變量path中
檢驗是否添加成功:
打開cmd,輸入 Chromedriver。如下圖,安裝成功。
思考:
上面都操作成功了,運行下面程序報錯