小编典典

在Selenium中使用XPath选择器'following-sibling :: text()'(Python)

selenium

我正在尝试使用Selenium(在Python中)从网站中提取一些信息。我一直在使用XPaths选择元素,但是在使用以下同级选择器时遇到了麻烦。HTML如下:

<span class="metadata">
    <strong>Photographer's Name: </strong>
    Ansel Adams
</span>

我可以选择“摄影师的名字”

In [172]: metaData = driver.find_element_by_class_name('metadata')

In [173]: metaData.find_element_by_xpath('strong').text
Out[173]: u"Photographer's Name:"

我试图选择标记后的文本部分(示例中为“ Ansel Adams”)。我以为我可以使用以下同级选择器,但收到以下错误:

In [174]: metaData.find_element_by_xpath('strong/following-sibling::text()')
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (328, 0))
... [NOTE: Omitted the traceback for brevity] ...
InvalidSelectiorException: Message: u'The given selector strong/following-sibling::text() is either invalid or does not result in a WebElement. The following error occurred:\n[InvalidSelectorError] The result of the xpath expression "strong/following-sibling::text()" is: [object Text]. It should be an element.'

有什么想法为什么这不起作用?


阅读 1739

收藏
2020-06-26

共1个答案

小编典典

@RossPatterson是正确的。麻烦在于文本“ Ansel Adams”不是a
WebElement,因此您不能使用find_elementfind_elements。如果您将HTML更改为

<span class="metadata">
    <strong>Photographer's Name: </strong>
    <strong>Ansel Adams</strong>
</span>

然后find_element_by_xpath('strong/following-sibling::*[1]').text返回“ Ansel
Adams”。

2020-06-26