[英]Using Python & Selenium, how to extract the text from HTML containing the <p> tag?
This I know is a very simple question. 我知道这是一个非常简单的问题。 I'm quite sick and trying to finish up this presentation and my brain just doesn't seem to be working right.
我很恶心,试图完成本演示文稿,但我的大脑似乎工作不正常。
The HTML code is as follows: HTML代码如下:
<p>
<b>Postal code:</b>
3502
</p>
The defect is the zipcode text field is only accepting four characters. 缺陷是邮政编码文本字段仅接受四个字符。 Once submitted, I'm trying to grab the number "3502" in this case and use len to count them.
提交后,在这种情况下,我尝试获取数字“ 3502”,并使用len进行计数。
The problem is that you cannot directly locate the "text" nodes with find_element_*
commands in selenium - the locators you use have to point to actual elements. 问题是您无法使用硒中的
find_element_*
命令直接定位“文本”节点-您使用的定位器必须指向实际元素。
In this case, I would get the p
element's text, split by :
and get the last item: 在这种情况下,我将获取
p
元素的文本,并用:
分割并得到最后一项:
text = driver.find_element_by_xpath("//p[b = 'Postal code:']").text
postal_code = text.split(":")[-1].strip()
print(postal_code)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.