使用Selenium获取段落元素的文本


`<div id="businessCategory12">`
`<p style="margin-top: 0px;line-height:80%;margin-left:5px;font-weight: bold;color:#00004C">Business Types</p>`
`<p style="margin-top: 0px;line-height:80%;margin-left:15px;font-weight: bold;">&nbsp;&nbsp;&nbsp;Minority Owned Business</p>`
`<p style="margin-top: 0px;line-height:80%;margin-left:15px;">&nbsp;&nbsp;&nbsp;Black American Owned</p>`
`</div>``

我正在为客户做一个网络抓取工具。我需要从上面使用硒(python)的第三段文字,但我有很多麻烦。文字应该是"美国黑人拥有"。我试过以下,但它一直给我一个空值。我哪里做错了?

任何帮助或其他方式获得文本将是非常非常感激!

`minority = driver.find_element_by_xpath("//*[@id='businessCategory12']/p[3]")`
`minority_owned = minority.text`

节点可能被隐藏尝试用textContent代替文本

minority = driver.find_element_by_xpath("//*[@id='businessCategory12']/p[3]")
minority_owned = minority.get_attribute("textContent")
<div id="businessCategory12">
<p style="margin-top: 0px;line-height:80%;margin-left:5px;font-weight: bold;color:#00004C">Business Types</p>
<p style="margin-top: 0px;line-height:80%;margin-left:15px;font-weight: bold;">Minority Owned Business</p>
<p style="margin-top: 0px;line-height:80%;margin-left:15px;">Black American Owned</p>
</div>

试试://p [3]/text ()

下面是一个很好的使用xpath的站点:https://scrapinghub.github.io/xpath-playground/

最新更新