首頁 >後端開發 >Python教學 >如何在 Python 中使用 Selenium 檢索 HTML 元素的屬性?

如何在 Python 中使用 Selenium 檢索 HTML 元素的屬性?

Mary-Kate Olsen
Mary-Kate Olsen原創
2024-11-03 11:53:03276瀏覽

How to Retrieve an HTML Element's Attribute Using Selenium in Python?

如何使用Selenium 取得HTML 元素的屬性

查詢:

在使用Selenium 的「Python 中,目標是Python

<code class="python">def test_chart_renders_from_url(self):
    url = 'http://localhost:8000/analyse/'
    self.browser.get(url)
    org = driver.find_element_by_id('org')
    # Retrieve org value?</code>

如何實作? Selenium 文件提供了有關元素選擇的充足信息,但沒有提供有關如何存取屬性的資訊。

回應:

要存取所需的屬性,請使用 get_attribute() 方法。下面提供了一個範例:

<code class="python">def test_chart_renders_from_url(self):
    url = 'http://localhost:8000/analyse/'
    self.browser.get(url)
    org = driver.find_element_by_id('org')
    # Retrieve org value?
    val = org.get_attribute("attribute name")</code>

以上是如何在 Python 中使用 Selenium 檢索 HTML 元素的屬性?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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