【发布时间】:2012-02-13 23:23:05
【问题描述】:
我正在尝试使用 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.'
关于为什么这不起作用的任何想法?
【问题讨论】: