【发布时间】:2017-02-24 16:48:34
【问题描述】:
我发现使用 python 2.7 和 selenium 很难获取 HTML 页面的 head 标记中包含的 HTML 注释标记 <!-- stuff --> 的内容。
<head>
<!-- I would like to get this sentence -->
[...]
</head>
我使用 FirePath/FireBug 获得了该评论的 XPath(所以我假设它是正确的):html/head/comment()[1]。
然后:
- 这个
given_driver.find_element_by_xpath('html/head/comment()[1]')给我InvalidSelectorException说Message: The given selector html/head/comment()[1] is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: The result of the xpath expression "html/head/comment()[1]" is: [object Comment]. It should be an element. - 这个
head_element = given_driver.find_element_by_xpath('html/head')然后给了我head标签中的整个HTML代码head_element.get_attribute('innerHTML')就像:u'<!-- I would like to get this sentence -->\n [...]
但我想只获取head 标签内的评论标签的内容。我想知道这对于 Selenium 是不可能的,但对我来说似乎很奇怪。我怎么能得到它?
【问题讨论】:
标签: html python-2.7 selenium comments