[英]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.