簡體   English   中英

如何使用 Selenium 從 Python 中的 html 標題獲取文本?

[英]How do I get text from an html heading in Python using Selenium?

我目前正在學習 Python,作為我自己的一個項目,我正在學習如何使用 Selenium 與網站進行交互。 我通過它在 HTML 中的 id 找到了該元素,但是當我不知道如何引用元素內的標題時。 在這種情況下,我只想要<h4>中的字符串。

 <div class="estimate-box equal-width" id="estimate-box"> <a href="#worth" id="worthBox" style="text-decoration: none;"> <h5>Worth</h5> <h4>$5.02</h4> </a> </div>

我的問題是,如何讓 python 僅提取<h4>中的文本? 如果我格式錯誤,我很抱歉,這是我的第一篇文章。 提前致謝!

使用以下 xpath。

print(driver.find_element_by_xpath("//a[id='worthBox']/h4").text)

或跟隨 css 選擇器。

print(driver.find_element_by_css_selector("#worthBox>h4").text)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM