天天看點

Python+Selenium浏覽器自動化操作

1.安裝selenium

pip install selenium # Windows
pip3 install selenium # Mac           

2.下載下傳浏覽器驅動

下載下傳位址:https://chromedriver.chromium.org/downloads

Python+Selenium浏覽器自動化操作

注意浏覽器版本

将下載下傳好的chromedriver_win32.zip解壓得到一個chromedriver.exe檔案,将其複制到你的Python程式執行路徑。

Python+Selenium浏覽器自動化操作

3.啟動 Chrome浏覽器的調試模式

  • 找到Chrome浏覽器安裝位置
Python+Selenium浏覽器自動化操作
  • 執行以下指令
chrome.exe --remote-debugging-port=9222           
Python+Selenium浏覽器自動化操作

4.示例代碼

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

配置 = Options()
配置.debugger_address = 'localhost:9222'
驅動位置 = "./Data/chromedriver.exe" #我是把chromedriver驅動放在項目根目錄下
浏覽器 = webdriver.Chrome(驅動位置, chrome_options=配置)
print(浏覽器.title)