DEV Community

codemee
codemee

Posted on • Edited on

8

Selenium 4 的使用

Selenium 4Selenium 3 主要有兩大差異, 以下分別說明。

WebDriver

從 Selenium 4.6 開始, 已經內建 Selenium manager, 它會在 PATH 指定的路徑下找不到 WebDriver 的執行檔時自動配置 WebDriver, 完全不需要自己下載檔案以及設定 PATH, 以下以 Firefox 為例:



>>> from selenium import webdriver
>>> driver = webdriver.Firefox()


Enter fullscreen mode Exit fullscreen mode

即可自動配置 WebDriver 並開啟安裝的 Firefox 瀏覽器。

使用 Selenium manager 是最簡單方便的作法, 但如果你被迫使用較舊的版本, 必須自行下載安裝 WebDriver, 也要注意在建立 webdriver 物件時, 如果你的 WebDriver 執行檔並不在 PATH 指定的路徑下, 過去用來指定路徑的 executable_path 參數已經被棄用, 要改用 Service 物件

不過自己要管理 WebDriver 執行檔其實有點麻煩, 不但要設定路徑, 還要跟著瀏覽器版本更新檔案, 比較簡單的方式是使用 webdriver-manager



pip install webdriver-manager


Enter fullscreen mode Exit fullscreen mode

安裝後即可如下建立 webdriver 物件, 此處以 Windows 內建的 Edge 瀏覽器為例:



>>> from selenium import webdriver
>>> from webdriver_manager.microsoft import EdgeChromiumDriverManager
>>> from selenium.webdriver.edge.service import Service
>>> driver = webdriver.Edge(
...     service=Service(EdgeChromiumDriverManager().install()))


Enter fullscreen mode Exit fullscreen mode

這個 webdriver-manager 套件也可以用在 Selenium 3 上, 以 Firefox 為例:



>>> from selenium import webdriver
>>> from webdriver_manager.firefox import GeckoDriverManager
>>> driver = webdriver.Firefox(executable_path=GeckoD
... riverManager().install())


Enter fullscreen mode Exit fullscreen mode

如果你的瀏覽器並不是安裝在預設的路徑上, 就必須自行在建立 webdriver 物件時以對應瀏覽器的 Option 物件指定瀏覽器執行檔的路徑。例如我有一個可攜式的 Chrome 位於 d:\apps\PortableApps\GoogleChromePortable\App\Chrome-bin\chrome.exe, 就可以透過以下方式建立 webdriver 物件:



>>> from selenium.webdriver.chrome.options import Options
>>> o = Options()
>>> o.binary_location = "d:\\apps\\PortableApps\\GoogleChromePortable\\App\\Chrome-bin\\chrome.exe"
>>> driver = webdriver.Chrome(options=o)


Enter fullscreen mode Exit fullscreen mode

取得元素

Selenium 4 不提供 find_element_by_XXX 的方法, 只提供取得第一個元素 find_element 或是所有元素的 find_elements 方法, 可以搭配 By 類別指定找尋元素的依據。

此處以 https://www.w3schools.com/html/html_tables.asp 為例:



>>> driver.get("https://www.w3schools.com/html/html_tables.asp")


Enter fullscreen mode Exit fullscreen mode

其中的表格 id 為 "customers", 若要取得格中的第一個儲存格, 可以這樣做:



>>> from selenium.webdriver.common.by import By
>>> td = driver.find_element(By.CSS_SELECTOR, "#customers td")
>>> td.text
'Alfreds Futterkiste'


Enter fullscreen mode Exit fullscreen mode

或是也可以分段先取得表格元素, 再取出表格中的所有儲存格, 然後再取出其中的第一個儲存格:



>>> tab = driver.find_element(By.ID, "customers")
>>> tds = tab.find_elements(By.TAG_NAME, "td")
>>> len(tds)
18

>>> tds[0].text
'Alfreds Futterkiste'


Enter fullscreen mode Exit fullscreen mode

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay