首頁  >  文章  >  後端開發  >  如何使用 Selenium 和 Python 提取 Shadow DOM 元素?

如何使用 Selenium 和 Python 提取 Shadow DOM 元素?

Patricia Arquette
Patricia Arquette原創
2024-10-19 06:42:30245瀏覽

How to Extract Shadow DOM Elements Using Selenium and Python?

使用Selenium Python 提取Shadow DOM

在提供的線上商店URL 中,產品資訊存在於#shadow-root 中開啟)元素。要使用 Selenium Python 存取此數據,我們需要使用 ShadowRoot.querySelector() 方法。

解決方案:

要擷取產品標籤,請使用以下程式碼:

<code class="python">driver.get('https://www.tiendasjumbo.co/buscar?q=mani')
item = driver.execute_script("return document.querySelector('impulse-search').shadowRoot.querySelector('div.group-name-brand h1.impulse-title span.formatted-text')")
print(item.text)</code>

此腳本導覽至指定的URL 並執行JavaScript以存取影子DOM 中的元素。

參考:

參考請參閱以下討論以獲取更多見解:

  • 無法使用Selenium 和Python 在#shadow-root (open) 中找到登入元素
  • 如何在其中找到名字欄位使用Selenium 和Python 在網站https://www.virustotal.com 中進行Shadow-root(開啟)

Microsoft Edge 與Google Chrome 版本96 的注意事項:

Chrome v96 修改了Selenium 的影子根回傳值。利用提供的資源來獲取有關此特定版本的指導。

以上是如何使用 Selenium 和 Python 提取 Shadow DOM 元素?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn