Python selenium 庫
Selenium 是一個(gè)用于自動化 Web 瀏覽器操作的強(qiáng)大工具,廣泛應(yīng)用于 Web 應(yīng)用程序測試、網(wǎng)頁數(shù)據(jù)抓取和任務(wù)自動化等場景。 Selenium 為各種編程語言提供了 API,用作測試。 目前的官方 API 文檔有 C#、JavaScript、Java、Python、Ruby。 Selenium 教程:https://www.runoob.com/selenium/ 安裝 Selenium 和 WebDriver安裝 Selenium要開始使用 Selenium,首先需要安裝 selenium 庫,并下載適用于你瀏覽器的 WebDriver。 使用 pip 安裝 Selenium: pip install selenium 安裝完成后,可以使用以下命令查看 selenium 的版本信息: pip show selenium 也可以使用 Python 代碼查看: import selenium print(selenium.__version__) 下載WebDriverSelenium 需要一個(gè) WebDriver 來與瀏覽器進(jìn)行交互。 不同的瀏覽器需要不同的 WebDriver,例如 Chrome 瀏覽器需要 ChromeDriver,你需要根據(jù)你使用的瀏覽器下載相應(yīng)的 WebDriver,并確保它在你的系統(tǒng) PATH 中。
選擇瀏覽器并初始化 WebDriver: 實(shí)例
from selenium import webdriver # 使用 Chrome 瀏覽器 driver = webdriver.Chrome(executable_path='/path/to/chromedriver') # 或者使用 Firefox 瀏覽器 # driver = webdriver.Firefox(executable_path='/path/to/geckodriver') # 或者使用 Edge 瀏覽器 # driver = webdriver.Edge(executable_path='/path/to/msedgedriver') 從 Selenium 4 開始,在瀏覽器驅(qū)動的管理方式上發(fā)生了變化:Selenium 4 嘗試自動檢測系統(tǒng)中安裝的瀏覽器版本,并下載相應(yīng)的驅(qū)動程序,這意味著用戶不再需要手動下載和設(shè)置驅(qū)動程序路徑,除非他們需要特定版本的驅(qū)動程序。 實(shí)例
from selenium import webdriver driver = webdriver.Chrome() # 如果使用其他瀏覽器,如 Firefox,需要相應(yīng)修改 當(dāng)國內(nèi)的網(wǎng)絡(luò)環(huán)境,自動檢測下載驅(qū)動需要不一樣的網(wǎng)絡(luò)環(huán)境,所以建議手動下載驅(qū)動,然后指定驅(qū)動路徑。 在 Selenium 4 中,不再直接在 webdriver.Chrome 中設(shè)置驅(qū)動程序路徑,而是通過引入 Service 對象來設(shè)置。這樣可以避免棄用警告,并確保驅(qū)動程序的正確加載。例如: 實(shí)例
from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService service = ChromeService(executable_path="PATH_TO_DRIVER") options = webdriver.ChromeOptions() driver = webdriver.Chrome(service=service, options=options) 以下代碼指定驅(qū)動文件路徑來獲取網(wǎng)頁標(biāo)題: 實(shí)例
from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService # 設(shè)置正確的驅(qū)動路徑 service = ChromeService(executable_path="/RUNOOB/Downloads/chromedriver-mac-arm64/chromedriver") options = webdriver.ChromeOptions() driver = webdriver.Chrome(service=service, options=options) # 打開一個(gè)網(wǎng)站 driver.get("https://cn.bing.com") # 獲取頁面標(biāo)題 print(driver.title) # 關(guān)閉瀏覽器 driver.quit() 基本用法初始化 WebDriver選擇瀏覽器并初始化 WebDriver: 實(shí)例
from selenium import webdriver # 使用 Chrome 瀏覽器 driver = webdriver.Chrome() # 或者使用 Firefox 瀏覽器 # driver = webdriver.Firefox() # 或者使用 Edge 瀏覽器 # driver = webdriver.Edge() 打開網(wǎng)頁使用 get() 方法打開網(wǎng)頁: driver.get("https://www.baidu.com")
查找頁面元素可以通過多種方式查找頁面元素,例如使用 ID、類名、標(biāo)簽名等: 實(shí)例
# 通過 ID 查找元素 search_box = driver.find_element("id", "kw") # 通過類名查找元素 search_button = driver.find_element("class name", "s_ipt") # 通過標(biāo)簽名查找元素 links = driver.find_elements("tag name", "a") 模擬用戶操作Selenium 可以模擬用戶在瀏覽器中的操作,例如點(diǎn)擊、輸入文本等: 實(shí)例
# 在搜索框中輸入文本 search_box.send_keys("Selenium Python") # 點(diǎn)擊搜索按鈕 search_button.click() 獲取元素屬性和文本可以獲取頁面元素的屬性值或文本內(nèi)容: 實(shí)例
# 獲取元素的文本 element_text = search_box.text # 獲取元素的屬性值 element_attribute = search_box.get_attribute("placeholder") 等待有時(shí)頁面加載需要時(shí)間,可以使用顯式等待或隱式等待來確保元素可操作: 實(shí)例
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 顯式等待 element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "kw")) ) # 隱式等待 driver.implicitly_wait(10) 關(guān)閉瀏覽器操作完成后,記得關(guān)閉瀏覽器: driver.quit() 簡單的網(wǎng)頁自動化下面是一個(gè)簡單的 Selenium 項(xiàng)目示例,用于自動化搜索關(guān)鍵詞,并獲取結(jié)果頁面的標(biāo)題。 實(shí)例
from selenium import webdriver from selenium.webdriver.common.by import By # 導(dǎo)入 By 模塊 from selenium.webdriver.chrome.service import Service as ChromeService from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 設(shè)置驅(qū)動的路徑,啟動瀏覽器 service = ChromeService(executable_path="/RUNOOB/Downloads/chromedriver-mac-arm64/chromedriver") options = webdriver.ChromeOptions() driver = webdriver.Chrome(service=service, options=options) try: # 打開百度首頁 driver.get("https://www.baidu.com") # 查找搜索框元素 search_box = driver.find_element(By.ID, "kw") # 輸入搜索內(nèi)容 search_box.send_keys("Selenium Python") # 提交搜索表單 search_box.send_keys(Keys.RETURN) # 等待搜索結(jié)果加載 WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "content_left")) ) # 打印頁面標(biāo)題 print("頁面標(biāo)題是:", driver.title) finally: # 關(guān)閉瀏覽器 driver.quit() selenium 常用方法下表列出了 selenium 庫的常用方法:
|
|
|