iT邦幫忙

2024 iThome 鐵人賽

DAY 11
0
Python

一些Python可以做的事系列 第 11

[Python] Selenium 取得和操作網頁元素

  • 分享至 

  • xImage
  •  

取得網頁元素

我們想要使用網頁元素,首先要載入 selenium 的 By 模組,接著使用 find_element() 或 find_elements() 搭配參數設定,取得指定的網頁元素

By 模組 :

from selenium.webdriver.common.by import By

查找元素 :

  • find_element(參數) => 查找網頁上的單個元素,返回第一個匹配元素
  • find_elements(參數) => 查找網頁上所有匹配的元素(可能會有多個),以串列方式回傳找到的元素

下列是一些常用參數 :

  • By.ID, id : 透過 id 尋找
  • By.CLASS_NAME, class : 透過 class 尋找
  • By.CSS_SELECTOR, css selector : 透過 css 選擇器尋找
  • By.NAME, name : 透過 name 屬性尋找
  • By.TAG_NAME, tag : 透過 HTML tag 尋找
  • By.LINK_TEXT, text : 透過超連結的文字尋找
  • By.PARTIAL_LINK_TEXT, text : 透過超連結的部分文字,尋找
  • By.XPATH, xpath : 透過 xpath 的方式尋找

實作

接下來我會照https://example.oxxostudio.tw/python/selenium/demo.html 這個開啟範例網址,去取得其網頁元素,並按照參考資料的程式碼去實作一次

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select   # 使用 Select 對應下拉選單
import time

driver = webdriver.Chrome()
driver.get('https://example.oxxostudio.tw/python/selenium/demo.html')  # 開啟範例網址

# 取得 id 為 a 的網頁元素 ( 按鈕 A )
a = driver.find_element(By.ID, 'a')    

# 取得 class 為 btn 的網頁元素 ( 按鈕 B )
b = driver.find_element(By.CLASS_NAME, 'btn') 

# 取得 class 為 test 的網頁元素 ( 按鈕 C )
c = driver.find_element(By.CSS_SELECTOR, '.test')  

# 取得屬性 name 為 dog 的網頁元素 ( 按鈕 D )
d = driver.find_element(By.NAME, 'dog')     

# 取得 tag 為 h1 的網頁元素
h1 = driver.find_element(By.TAG_NAME, 'h1')   

# 取得指定超連結文字的網頁元素
link1 = driver.find_element(By.LINK_TEXT, '我是超連結,點擊會開啟 Google 網站')  

# 取得超連結文字包含 Google 的網頁元素
link2 = driver.find_element(By.PARTIAL_LINK_TEXT, 'Google') 

# 取得 html > body > select 這個網頁元素
select = Select(driver.find_element(By.XPATH, '/html/body/select'))   \

操作網頁元素

使用 Selenium 函式庫操作網頁元素下列幾種常見方法 :

  • click(element) : 按下滑鼠左鍵
  • click_and_hold(element) : 滑鼠左鍵按著不放
  • double_click(element) : 連續按兩下滑鼠左鍵
  • send_keys(values) : 送出某個鍵盤按鍵值
  • send_keys_to_element(element, values) : 向某個元素發送鍵盤按鍵值
  • key_down(value) : 按著鍵盤某個鍵
  • pause(seconds) : 暫停動作
  • element.clear() : 清除輸入欄位的當前文本
  • text = element.text : 獲取元素的文本內容
  • visible = element.is_displayed() : 檢查元素是否可見
  • value = element.get_attribute(‘attribute’) : 獲取元素的指定屬性值

要使用這些方法有兩種方式 :

  1. 針對指定元素呼叫方法
  2. 使用「ActionChains」,將所有需要執行的方法串成「鏈」,全部完成後執行 perform() 執行所有的過程

實作

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep

driver = webdriver.Chrome()
driver.get('https://example.oxxostudio.tw/python/selenium/demo.html')
a = driver.find_element(By.ID, 'a')
c = driver.find_element(By.CLASS_NAME, 'test')
add = driver.find_element(By.ID, 'add')

# 1. 針對指定元素呼叫方法,例如 click()
a.click()
sleep(5)

# 2. 使用 ActionChains 的方式
actions = ActionChains(driver)
actions.click(c).pause(1)
actions.double_click(add).pause(1).click(add).pause(1).click(add)

# 最後執行儲存的動作
actions.perform()

參考資料
https://steam.oxxostudio.tw/category/python/spider/selenium.html#a4
https://utrustcorp.com/python-selenium/


上一篇
[Python] Selenium 函式庫
下一篇
[Python] Selenium 點擊和輸入
系列文
一些Python可以做的事19
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言