<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>瀏覽器 on IT 空間</title><link>https://blog.jiatool.com/tags/%E7%80%8F%E8%A6%BD%E5%99%A8/</link><description>Recent content in 瀏覽器 on IT 空間</description><generator>Hugo -- gohugo.io</generator><language>zh</language><managingEditor>jia@jiatool.com (Jia)</managingEditor><webMaster>jia@jiatool.com (Jia)</webMaster><copyright>&amp;copy;{year}, Jia All Rights Reserved</copyright><lastBuildDate>Mon, 30 Mar 2026 21:15:00 +0800</lastBuildDate><atom:link href="https://blog.jiatool.com/tags/%E7%80%8F%E8%A6%BD%E5%99%A8/index.xml" rel="self" type="application/rss+xml"/><item><title>「YouTube 播放清單篩選器」瀏覽器擴充功能(Extension)</title><link>https://blog.jiatool.com/posts/youtube-playlist-filter/</link><pubDate>Mon, 30 Mar 2026 21:15:00 +0800</pubDate><author>jia@jiatool.com (Jia)</author><atom:modified>Mon, 30 Mar 2026 21:15:00 +0800</atom:modified><guid>https://blog.jiatool.com/posts/youtube-playlist-filter/</guid><description>前言 不知道大家是不是都一樣，我 YouTube 的「稍後觀看」清單裡面影片數量高達 7、800 部 (我有努力在清了&amp;hellip;)，此時有時想過濾某頻道的影片</description><content:encoded>&lt;h2 id="前言">前言&lt;/h2>
&lt;p>不知道大家是不是都一樣，我 YouTube 的「稍後觀看」清單裡面影片數量高達 7、800 部 (我有努力在清了&amp;hellip;)，此時有時想過濾某頻道的影片，或有個短時間的空檔，想找 10 分鐘以內的影片填充，原生網頁沒有任何篩選工具，就像大海撈針&amp;hellip;&lt;/p>
&lt;p>最近我就想到，現在 Vibe Coding 當道，自己做一款這種用途的瀏覽器擴充功能(Extension) 應該不難，因此就誕生出「YouTube 播放清單篩選器」~&lt;/p>
&lt;br/>
&lt;div class="notices info" data-title="連結">
Chrome 線上應用程式商店：&lt;br />
&lt;a href="https://chromewebstore.google.com/detail/hbffnboipbkgkphpnjddfaofiahmcolh">「YouTube 播放清單篩選器」擴充功能&lt;/a>
&lt;/div>
&lt;figure >
&lt;img data-src="https://res.cloudinary.com/jiablog/youtube_playlist_filter/scrolling_image.jpg" alt="「YouTube 播放清單篩選器」瀏覽器擴充功能" data-caption="「YouTube 播放清單篩選器」瀏覽器擴充功能" src="data:image/svg+xml,%0A%3Csvg xmlns='http://www.w3.org/2000/svg' width='600px' height='' viewBox='0 0 24 24'%3E%3Cpath fill='none' d='M0 0h24v24H0V0z'/%3E%3Cpath fill='%23aaa' d='M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-1 16H6c-.55 0-1-.45-1-1V6c0-.55.45-1 1-1h12c.55 0 1 .45 1 1v12c0 .55-.45 1-1 1zm-4.44-6.19l-2.35 3.02-1.56-1.88c-.2-.25-.58-.24-.78.01l-1.74 2.23c-.26.33-.02.81.39.81h8.98c.41 0 .65-.47.4-.8l-2.55-3.39c-.19-.26-.59-.26-.79 0z'/%3E%3C/svg%3E" class="lazyload" style="width:600px;height:;"/>
&lt;figcaption style="text-align: center;">
「YouTube 播放清單篩選器」瀏覽器擴充功能
&lt;/figcaption>
&lt;/figure>
&lt;br/>
&lt;h2 id="介紹">介紹&lt;/h2>
&lt;p>前往 Chrome 線上應用程式商店 找到「&lt;a href="https://chromewebstore.google.com/detail/hbffnboipbkgkphpnjddfaofiahmcolh" target="_blank" rel="noopener">
YouTube 播放清單篩選器
&lt;/a>」擴充功能 (Edge 應該也可以安裝使用)。&lt;/p>
&lt;br/>
&lt;p>完成後前往 YouTube 的「稍後觀看」頁面 (或任何「播放清單」頁面)，上方就會出現此工具 UI。&lt;/p>
&lt;figure >
&lt;img data-src="https://res.cloudinary.com/jiablog/youtube_playlist_filter/screenshot0.jpg" alt="在「稍後觀看」頁面上方會顯示此工具 UI" data-caption="在「稍後觀看」頁面上方會顯示此工具 UI" src="data:image/svg+xml,%0A%3Csvg xmlns='http://www.w3.org/2000/svg' width='900px' height='' viewBox='0 0 24 24'%3E%3Cpath fill='none' d='M0 0h24v24H0V0z'/%3E%3Cpath fill='%23aaa' d='M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-1 16H6c-.55 0-1-.45-1-1V6c0-.55.45-1 1-1h12c.55 0 1 .45 1 1v12c0 .55-.45 1-1 1zm-4.44-6.19l-2.35 3.02-1.56-1.88c-.2-.25-.58-.24-.78.01l-1.74 2.23c-.26.33-.02.81.39.81h8.98c.41 0 .65-.47.4-.8l-2.55-3.39c-.19-.26-.59-.26-.79 0z'/%3E%3C/svg%3E" class="lazyload" style="width:900px;height:;"/>
&lt;figcaption style="text-align: center;">
在「稍後觀看」頁面上方會顯示此工具 UI
&lt;/figcaption>
&lt;/figure>
&lt;h3 id="-核心功能">🌟 核心功能&lt;/h3>
&lt;ol>
&lt;li>頻道篩選：自動抓取清單中的所有頻道，只想看哪個特定創作者的影片直接過濾。&lt;/li>
&lt;li>影片長度過濾：
&lt;ul>
&lt;li>滑桿控制：自由設定影片時間長度範圍。&lt;/li>
&lt;li>快捷按鈕：預設「5分鐘內」、「10-30分鐘」&amp;hellip;等常用區間，快速過濾。&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>關鍵字搜尋：輸入關鍵字，搜尋影片標題和頻道名稱。&lt;/li>
&lt;li>多種排序：支援依影片時長、觀看次數、頻道名稱進行升序/降序排列。&lt;/li>
&lt;li>全自動加載：一鍵加載所有播放清單影片，無需手動慢慢向下滾動。&lt;/li>
&lt;li>色彩主題：支援「淺色」與「深色」主題，自動依照你 YouTube 的設定切換。&lt;/li>
&lt;li>佈局優化：提供「預設」與「緊湊」兩種顯示佈局，「緊湊」模式可以在相同畫面下呈現更多影片。&lt;/li>
&lt;/ol>
&lt;br>
&lt;figure >
&lt;img data-src="https://res.cloudinary.com/jiablog/youtube_playlist_filter/screenshot2.jpg" alt="支援「淺色」與「深色」主題" data-caption="支援「淺色」與「深色」主題" src="data:image/svg+xml,%0A%3Csvg xmlns='http://www.w3.org/2000/svg' width='700px' height='' viewBox='0 0 24 24'%3E%3Cpath fill='none' d='M0 0h24v24H0V0z'/%3E%3Cpath fill='%23aaa' d='M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-1 16H6c-.55 0-1-.45-1-1V6c0-.55.45-1 1-1h12c.55 0 1 .45 1 1v12c0 .55-.45 1-1 1zm-4.44-6.19l-2.35 3.02-1.56-1.88c-.2-.25-.58-.24-.78.01l-1.74 2.23c-.26.33-.02.81.39.81h8.98c.41 0 .65-.47.4-.8l-2.55-3.39c-.19-.26-.59-.26-.79 0z'/%3E%3C/svg%3E" class="lazyload" style="width:700px;height:;"/>
&lt;figcaption style="text-align: center;">
支援「淺色」與「深色」主題
&lt;/figcaption>
&lt;/figure>
&lt;figure >
&lt;img data-src="https://res.cloudinary.com/jiablog/youtube_playlist_filter/screenshot3.jpg" alt="「緊湊」佈局可以在相同畫面下呈現更多影片" data-caption="「緊湊」佈局可以在相同畫面下呈現更多影片" src="data:image/svg+xml,%0A%3Csvg xmlns='http://www.w3.org/2000/svg' width='700px' height='' viewBox='0 0 24 24'%3E%3Cpath fill='none' d='M0 0h24v24H0V0z'/%3E%3Cpath fill='%23aaa' d='M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-1 16H6c-.55 0-1-.45-1-1V6c0-.55.45-1 1-1h12c.55 0 1 .45 1 1v12c0 .55-.45 1-1 1zm-4.44-6.19l-2.35 3.02-1.56-1.88c-.2-.25-.58-.24-.78.01l-1.74 2.23c-.26.33-.02.81.39.81h8.98c.41 0 .65-.47.4-.8l-2.55-3.39c-.19-.26-.59-.26-.79 0z'/%3E%3C/svg%3E" class="lazyload" style="width:700px;height:;"/>
&lt;figcaption style="text-align: center;">
「緊湊」佈局可以在相同畫面下呈現更多影片
&lt;/figcaption>
&lt;/figure>
&lt;br>
&lt;h3 id="-隱私與資安">🔒 隱私與資安&lt;/h3>
&lt;p>並且為了確保隱私與資安，它是 100% 都在你的瀏覽器本地運行，我也完全不收集任何數據，大家可以放心使用。&lt;/p>
&lt;br>
&lt;figure >
&lt;img data-src="https://res.cloudinary.com/jiablog/youtube_playlist_filter/approval_successful.jpg" alt="等了一個禮拜，通過審查並上架成功~" data-caption="等了一個禮拜，通過審查並上架成功~" src="data:image/svg+xml,%0A%3Csvg xmlns='http://www.w3.org/2000/svg' width='450px' height='' viewBox='0 0 24 24'%3E%3Cpath fill='none' d='M0 0h24v24H0V0z'/%3E%3Cpath fill='%23aaa' d='M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-1 16H6c-.55 0-1-.45-1-1V6c0-.55.45-1 1-1h12c.55 0 1 .45 1 1v12c0 .55-.45 1-1 1zm-4.44-6.19l-2.35 3.02-1.56-1.88c-.2-.25-.58-.24-.78.01l-1.74 2.23c-.26.33-.02.81.39.81h8.98c.41 0 .65-.47.4-.8l-2.55-3.39c-.19-.26-.59-.26-.79 0z'/%3E%3C/svg%3E" class="lazyload" style="width:450px;height:;"/>
&lt;figcaption style="text-align: center;">
等了一個禮拜，通過審查並上架成功~
&lt;/figcaption>
&lt;/figure>
&lt;br>
&lt;!--adsense-->
&lt;br/>
&lt;h2 id="結語">結語&lt;/h2>
&lt;p>此擴充功能一開始是我自己想用，不過應該也有不少人有這個需求，所以我再花了一些時間 (還有花 150 元才能成為開發人員&amp;hellip;) 上架到官方，歡迎大家多多使用。&lt;/p>
&lt;p>如果有任何問題或建議，都可以留言或發 Email 反饋給我~&lt;/p>
&lt;br/>
&lt;p>對生成式 AI 感興趣的讀者，記得追蹤 FB 粉專『&lt;a href="https://www.facebook.com/jiatool" target="_blank" rel="noopener">
IT空間
&lt;/a>』，以免錯過最新的發文通知呦~🔔&lt;/p>
&lt;br/>
&lt;br/>
&lt;hr />
&lt;p>參考：&lt;/p>
&lt;br/>
&lt;blockquote>
&lt;p>「真正的死亡，是世界上再沒有一個人記得你。」&lt;br />
The real death is that no one in the world remembers you.&lt;/p>
&lt;p align="right">—— 《可可夜總會》&lt;/p>
&lt;/blockquote></content:encoded><dc:creator>Jia</dc:creator><media:content url="https://blog.jiatool.comimages/cover/youtube_playlist_filter.jpg" medium="image"><media:title type="html">featured image</media:title></media:content><media:content url="https://blog.jiatool.comimages/posts/youtube_playlist_filter_meta.jpg" medium="image"><media:title type="html">meta image</media:title></media:content><category>網頁</category><category>YouTube</category><category>瀏覽器</category><category>自製小工具</category></item><item><title>Playwright 瀏覽器自動化工具，應用於網路爬蟲和測試</title><link>https://blog.jiatool.com/posts/playwright/</link><pubDate>Sat, 20 Nov 2021 21:00:00 +0800</pubDate><author>jia@jiatool.com (Jia)</author><atom:modified>Sat, 20 Nov 2021 21:00:00 +0800</atom:modified><guid>https://blog.jiatool.com/posts/playwright/</guid><description>前言 你還在使用 Selenium 來網路爬蟲或自動化測試嗎？或許你可以來試試另一個工具 — Playwright。 (圖片來源：Unsplash) [小廣告] 我製作了</description><content:encoded>&lt;h2 id="前言">前言&lt;/h2>
&lt;p>你還在使用 Selenium 來網路爬蟲或自動化測試嗎？或許你可以來試試另一個工具 — Playwright。&lt;/p>
&lt;figure >
&lt;img data-src="https://images.unsplash.com/photo-1486312338219-ce68d2c6f44d?w=600&amp;amp;q=50" alt="(圖片來源：Unsplash)" data-caption="(圖片來源：Unsplash)" src="data:image/svg+xml,%0A%3Csvg xmlns='http://www.w3.org/2000/svg' width='500px' height='' viewBox='0 0 24 24'%3E%3Cpath fill='none' d='M0 0h24v24H0V0z'/%3E%3Cpath fill='%23aaa' d='M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-1 16H6c-.55 0-1-.45-1-1V6c0-.55.45-1 1-1h12c.55 0 1 .45 1 1v12c0 .55-.45 1-1 1zm-4.44-6.19l-2.35 3.02-1.56-1.88c-.2-.25-.58-.24-.78.01l-1.74 2.23c-.26.33-.02.81.39.81h8.98c.41 0 .65-.47.4-.8l-2.55-3.39c-.19-.26-.59-.26-.79 0z'/%3E%3C/svg%3E" class="lazyload" style="width:500px;height:;"/>
&lt;figcaption style="text-align: center;">
(圖片來源：Unsplash)
&lt;/figcaption>
&lt;/figure>
&lt;br/>
&lt;div class="alert alert-info" role="alert" data-dir="ltr">[小廣告] 我製作了一款可愛的「&lt;a href="https://blog.jiatool.com/posts/penguin_wizard_sticker">企鵝魔法師&lt;/a>」貼圖~&lt;br />
歡迎下載：&lt;a href="https://line.me/S/sticker/31703222">LINE 貼圖&lt;/a>、&lt;a href="https://t.me/addstickers/penguin_wizard">Telegram 貼圖 (免費)&lt;/a>&lt;/p>
&lt;img src="https://res.cloudinary.com/jiablog/penguin_wizard_sticker/show.png" caption="企鵝魔法師" width="600px" position="center">&lt;/div>
&lt;h2 id="playwright-套件簡介">Playwright 套件簡介&lt;/h2>
&lt;p>Playwright 是微軟 Microsoft 開發的一個開源瀏覽器自動化工具，可以選擇 Chromium、Firefox、WebKit 瀏覽器，目前有 Python、JavaScript、TypeScript、.NET、Java 的 API 可供使用。&lt;/p>
&lt;p>* 本篇文章以 Python 為範例，但各程式語言的 API 應該是差不多的。&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>&lt;/th>
&lt;th>Linux&lt;/th>
&lt;th>macOS&lt;/th>
&lt;th>Windows&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Chromium 98.0.4708.0&lt;/td>
&lt;td>✅&lt;/td>
&lt;td>✅&lt;/td>
&lt;td>✅&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>WebKit 15.4&lt;/td>
&lt;td>✅&lt;/td>
&lt;td>✅&lt;/td>
&lt;td>✅&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Firefox 94.0.1&lt;/td>
&lt;td>✅&lt;/td>
&lt;td>✅&lt;/td>
&lt;td>✅&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>* 此版本會持續更新，以官方文檔為主。&lt;/p>
&lt;br/>
&lt;p>Playwright 第一次輸入指令就會自動下載瀏覽器，與自己使用的瀏覽器是分開的。不用像 Selenium 要手動下載，版本更新還要去更新驅動。&lt;/p>
&lt;br/>
&lt;p>Playwright 在對元素(elements)操作之前，會自動等待元素準備完成(可見的、啟用的&amp;hellip;)，不用怕元素還沒載入就執行造成錯誤，也不需要自己加等待。&lt;/p>
&lt;p>就像省去了 Selenium 內的 WebDriverWait：&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="n">WebDriverWait&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="bp">self&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">driver&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">timeout&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">until&lt;/span>&lt;span class="p">(&lt;/span>
&lt;span class="n">EC&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">presence_of_element_located&lt;/span>&lt;span class="p">((&lt;/span>&lt;span class="n">By&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">CSS_SELECTOR&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;#search&amp;#39;&lt;/span>&lt;span class="p">))&lt;/span>
&lt;span class="p">)&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>* 詳細可以參考官方文檔：&lt;a href="https://playwright.dev/python/docs/actionability">https://playwright.dev/python/docs/actionability&lt;/a>&lt;/p>
&lt;br/>
&lt;p>* Playwright 支援同步和異步(搭配 asyncio)，本篇文章皆使用同步來示範。&lt;br />
* Playwright 甚至能自動產生程式碼，這部分可以參考大數學堂的影片教學：&lt;a href="https://www.youtube.com/watch?v=moeAmM-7ybE" target="_blank" rel="noopener">
[爬蟲實戰] 如何不寫任何一行程式碼透過低代碼Low-Code / No-Code 工具 Playwright撰寫網頁自動化瀏覽程式
&lt;/a>&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://playwright.dev/python/" target="_blank" rel="noopener">
Playwright 官方網站
&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://playwright.dev/python/docs/intro" target="_blank" rel="noopener">
Playwright 文檔
&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://playwright.dev/python/docs/api/class-playwright" target="_blank" rel="noopener">
Playwright API
&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://github.com/microsoft/playwright-python" target="_blank" rel="noopener">
Playwright for Python GitHub
&lt;/a>&lt;/li>
&lt;/ul>
&lt;br/>
&lt;br/>
&lt;h2 id="教學">教學&lt;/h2>
&lt;p>先來試試官方文檔內的基本程式：&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;span class="lnt">6
&lt;/span>&lt;span class="lnt">7
&lt;/span>&lt;span class="lnt">8
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="kn">from&lt;/span> &lt;span class="nn">playwright.sync_api&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">sync_playwright&lt;/span>
&lt;span class="k">with&lt;/span> &lt;span class="n">sync_playwright&lt;/span>&lt;span class="p">()&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="n">p&lt;/span>&lt;span class="p">:&lt;/span>
&lt;span class="n">browser&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">p&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">chromium&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">launch&lt;/span>&lt;span class="p">()&lt;/span>
&lt;span class="n">page&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">browser&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">new_page&lt;/span>&lt;span class="p">()&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">goto&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;http://playwright.dev&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;span class="k">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">title&lt;/span>&lt;span class="p">())&lt;/span>
&lt;span class="n">browser&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">close&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>首先創造一個瀏覽器實例，開新分頁，再來前往 &lt;code>http://playwright.dev&lt;/code> 網頁，並取得網頁的 title，最後關閉瀏覽器。&lt;/p>
&lt;br/>
&lt;p>它預設是以「無頭模式」啟動，但為了在寫程式時觀察它運作，我們可以先將其關閉：&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="n">browser&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">p&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">chromium&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">launch&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">headless&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="bp">False&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;br/>
&lt;h3 id="常見的基本操作">常見的基本操作&lt;/h3>
&lt;p>以下是幾種常見的網頁基本操作：&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;span class="lnt">6
&lt;/span>&lt;span class="lnt">7
&lt;/span>&lt;span class="lnt">8
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="c1"># 點擊&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">click&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;div&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;span class="c1"># 輸入文字&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">fill&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;#search&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;query&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;span class="c1"># 複選框和單選按鈕&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">check&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;#agree&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;span class="c1"># 下拉選擇欄&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">select_option&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;select#colors&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;blue&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>更多進階用法與參數可參考官方文檔：&lt;/p>
&lt;ul>
&lt;li>滑鼠 Mouse：&lt;a href="https://playwright.dev/python/docs/api/class-mouse">https://playwright.dev/python/docs/api/class-mouse&lt;/a>&lt;/li>
&lt;li>鍵盤 Keyboard：&lt;a href="https://playwright.dev/python/docs/api/class-keyboard">https://playwright.dev/python/docs/api/class-keyboard&lt;/a>&lt;/li>
&lt;/ul>
&lt;br/>
&lt;h3 id="元素定位">元素定位&lt;/h3>
&lt;p>基本操作或取得元素皆使用 CSS Selector 元素定位。&lt;/p>
&lt;div class="alert alert-info" role="alert" data-dir="ltr">關於 CSS Selector (選擇器)的寫法可以參考我以前整理的文章：&lt;br />
&lt;a href="https://blog.jiatool.com/posts/sorted-out-css-selector/">常用網頁 CSS Selector (選擇器)取得元素，搭配 Python BeautifulSoup 套件&lt;/a>&lt;/div>
&lt;br/>
&lt;p>Playwright 除了有一般的 Selector 用法以外，還有個比較特別且好用的 &amp;quot;文字(text)&amp;quot; 選擇方式：&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;span class="lnt">17
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="c1"># 取得包含此段文字的元素 (不分大小寫)&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">click&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;text=Log in&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;span class="c1"># 取得與此段文字一模一樣的元素 (注意裡面還有引號)&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">click&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;text=&amp;#34;Log in&amp;#34;&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;span class="c1"># 如果要與 CSS Selector 結合，要使用以下方式&lt;/span>
&lt;span class="c1"># 概念同等於上方的 &amp;#39;text=Log in&amp;#39;，包含此段文字 (不分大小寫)&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">click&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;button:text(&amp;#34;Log in&amp;#34;)&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;span class="c1"># 概念同等於上方的 &amp;#39;text=&amp;#34;Log in&amp;#34;&amp;#39;，文字要完全一樣&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">click&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;button:text-is(&amp;#34;Log in&amp;#34;)&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;span class="c1"># 使用正則表達式&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">click&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;button:text-matches(&amp;#34;[+-]?&lt;/span>&lt;span class="se">\\&lt;/span>&lt;span class="s1">d+&amp;#34;)&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;span class="c1"># 還有一種是匹配內部包含文字的任何元素，可能在子元素或後代元素中&lt;/span>
&lt;span class="c1"># 例如底下會匹配 &amp;lt;article&amp;gt;&amp;lt;div&amp;gt;Playwright&amp;lt;/div&amp;gt;&amp;lt;/article&amp;gt;。&lt;/span>
&lt;span class="n">element&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">wait_for_selector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;article:has-text(&amp;#34;Playwright&amp;#34;)&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>在文字匹配會對空格進行正規化，例如將多個空格轉換為一個，將換行符變成空格並忽略開頭和結尾的空格。&lt;/p>
&lt;p>因為這部分有多種狀況、較複雜，我只是初步嘗試，如有錯誤歡迎底下留言告知。&lt;br />
參考：&lt;a href="https://playwright.dev/python/docs/selectors/#text-selector">https://playwright.dev/python/docs/selectors/#text-selector&lt;/a>&lt;/p>
&lt;br/>
&lt;p>以及一些其他的選擇方式：&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="c1"># 返回 &amp;lt;article&amp;gt; 具有 &amp;lt;div class=promo&amp;gt; 內部元素的文本內容&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">text_content&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;article:has(div.promo)&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;span class="c1"># 僅點擊可見 class=button 的元素&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">click&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;.button:visible&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;br/>
&lt;h3 id="選取元素">選取元素&lt;/h3>
&lt;p>如果要取得元素，可以使用：&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="c1"># 取得元素&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">query_selector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;#promo&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;span class="c1"># 取得元素，加入等待&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">wait_for_selector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;#promo&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;br/>
&lt;p>獲取元素的文字及屬性：&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="c1"># 取得元素的文字&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">query_selector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;h3&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">text_content&lt;/span>&lt;span class="p">()&lt;/span>
&lt;span class="c1"># 取得元素的屬性(attribute)&lt;/span>
&lt;span class="n">page&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">query_selector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;section&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get_attribute&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;class&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;br/>
&lt;h3 id="瀏覽器參數">瀏覽器參數&lt;/h3>
&lt;p>在教學一開始說到無頭模式，在 &lt;code>launch&lt;/code> 加入 &lt;code>headless=False&lt;/code>，它還有其他參數可以設定。&lt;br />
詳見：&lt;a href="https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch">https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch&lt;/a>&lt;/p>
&lt;p>像是傳遞給瀏覽器實例的附加參數，想讓它靜音就使用：&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="n">browser&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">playwright&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">chromium&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">launch&lt;/span>&lt;span class="p">(&lt;/span>
&lt;span class="n">args&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s2">&amp;#34;--mute-audio&amp;#34;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="c1"># 靜音&lt;/span>
&lt;span class="p">)&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>更多 Chromium flags 的列表：&lt;a href="https://peter.sh/experiments/chromium-command-line-switches/">https://peter.sh/experiments/chromium-command-line-switches/&lt;/a>&lt;/p>
&lt;br/>
&lt;h3 id="cookies-和-local-storage">Cookies 和 Local Storage&lt;/h3>
&lt;p>儲存與載入 Cookies 和 Local Storage，可以用作保存登入狀態。&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="c1"># 將存儲狀態保存到文件中&lt;/span>
&lt;span class="n">storage&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">context&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">storage_state&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">path&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;state.json&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;span class="c1"># 使用保存的存儲狀態創建新上下文(context)&lt;/span>
&lt;span class="n">context&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">browser&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">new_context&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">storage_state&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;state.json&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;br/>
&lt;!--adsense-->
&lt;h2 id="結語">結語&lt;/h2>
&lt;p>它還有個 GUI 工具 Trace Viewer，可以在程式執行後幫助探索操作過程，感覺非常的厲害，但這部分我就還沒嘗試過，有興趣可以參考：&lt;a href="https://playwright.dev/python/docs/trace-viewer">https://playwright.dev/python/docs/trace-viewer&lt;/a>&lt;/p>
&lt;br/>
&lt;p>Playwright 相比 Selenium 來說是比較新出來的工具(不過都已經是正式版了)，許多功能更好用，如果你原先是使用 Selenium 來寫網路爬蟲或做自動化測試，或許可以試試 Playwright，搞不好你就會愛上囉。😆&lt;/p>
&lt;br/>
&lt;p>歡迎追蹤『&lt;a href="https://www.facebook.com/jiatool" target="_blank" rel="noopener">
IT空間
&lt;/a>』FB 粉專，取得最新發文通知🔔&lt;/p>
&lt;br/>
&lt;br/>
&lt;hr />
&lt;p>參考：&lt;br />
&lt;a href="https://playwright.dev/python/" target="_blank" rel="noopener">
Playwright 官方網站
&lt;/a>&lt;br />
&lt;a href="https://playwright.dev/python/docs/intro" target="_blank" rel="noopener">
Playwright 文檔
&lt;/a>&lt;br />
&lt;a href="https://playwright.dev/python/docs/api/class-playwright" target="_blank" rel="noopener">
Playwright API
&lt;/a>&lt;br />
&lt;a href="https://github.com/microsoft/playwright-python" target="_blank" rel="noopener">
Playwright for Python GitHub
&lt;/a>&lt;/p>
&lt;br/>
&lt;blockquote>
&lt;p>對於自己想做的事，就放手去挑戰吧！不是因為覺得能成功就去做，而是發自內心的想要嘗試看看，這樣不管結果如何，自己都不會後悔。&lt;/p>
&lt;p align="right">—— 鈴木一朗 (棒球選手)&lt;/p>
&lt;/blockquote></content:encoded><dc:creator>Jia</dc:creator><media:content url="https://blog.jiatool.comimages/cover/playwright.jpg" medium="image"><media:title type="html">featured image</media:title></media:content><media:content url="https://blog.jiatool.comimages/posts/playwright_meta.jpg" medium="image"><media:title type="html">meta image</media:title></media:content><category>Playwright</category><category>瀏覽器</category><category>自動化</category><category>套件</category><category>Python</category><category>分享</category><category>Python套件</category></item></channel></rss>