Python Selenium 瀏覽器自動化測試工具
前言
Selenium 是一套「瀏覽器自動化測試工具」,主要用於: Web 自動化測試 網頁操作模擬 自動登入 自動填表 網頁資料爬取(Web Scraping)。它可以模擬使用者在瀏覽器中的操作,例如: 點擊按鈕 輸入文字 切換頁面
參考: https://www.selenium.dev/documentation/
安裝所需套件
pip3 install selenium
測試網頁
http://automated.pythonanywhere.com
測試代碼
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.common.exceptions import WebDriverException import time def get_driver(): # 增加啓動chrome參數 chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--headless") # 啓動無頭模式, 不會開啓瀏覽器視窗 chrome_options.add_argument("--disable-infobars") chrome_options.add_argument('--start-maximized') # 啓動後最大化視窗 chrome_options.add_argument('--no-sandbox') # 以最高權限運行 chrome_options.add_argument('--disable-dev-shm-usage') # 增加實驗性質的設置參數 chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) chrome_options.add_argument("--disable-blink-features=AutomationControlled")