简体   繁体   English

如何使用 Selenium 从 Python 中的 html 标题获取文本?

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

I'm currently learning Python, and as a project for myself, I'm learning how to use Selenium to interact with websites.我目前正在学习 Python,作为我自己的一个项目,我正在学习如何使用 Selenium 与网站进行交互。 I have found the element through its id in HTML, but when I don't know how to reference the heading inside the element.我通过它在 HTML 中的 id 找到了该元素,但是当我不知道如何引用元素内的标题时。 In this case, I just want the string from <h4> .在这种情况下,我只想要<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>

My question is, how do I get python to extract just the text in <h4> ?我的问题是,如何让 python 仅提取<h4>中的文本? I'm sorry if I formatted this wrong, this is my first post.如果我格式错误,我很抱歉,这是我的第一篇文章。 Thanks in advance!提前致谢!

Use following xpath.使用以下 xpath。

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

Or following css selector.或跟随 css 选择器。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在此 html 中使用 Selenium/Python 编写一些文本 - How do I write some text using Selenium/Python in this html 如何使用Selenium Python从html表中获取文本对象 - How to get text object from a html table using selenium python 如何使用 selenium python 从下面的 HTML 获取文本属性 - how to get the text attribute from the below HTML using selenium python 如何<span id>使用 Python、Selenium 和 BeautifulSoup</span>从 HTML 中抓取此文本<span id>?</span> - How do I scrape this text from an HTML <span id> using Python, Selenium, and BeautifulSoup? 如何使用Selenium在python中获取html文本? - How to get the html text in python using selenium? 如何使用 Python Selenium 获取元素内部的文本? - How do I get the text inside of an element using Python Selenium? "使用 python 和 selenium 从 HTML 表中获取特定的文本值" - Get a specific text value from an HTML table using python and selenium 如何使用python和selenium读取HTML表格单元格中的文本? - How do I read text in HTML table cells using python and selenium? 如何使用Python使用Selenium Webdriver获取此html中的一些文本值? - How to get the some text value in this html with Selenium Webdriver using Python? 如何使用 selenium 和 python 从网站获取工具提示文本,其中文本来自 javascript - How can I get the tooltip text from a website using selenium and python where the text comes from a javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM