【发布时间】:2015-12-08 07:46:28
【问题描述】:
我有一个网络应用程序。我正在使用 selenium 对其进行测试。我有一个节点
<span>Profile Run <span class="current">23</span> of 29</span>
我想读“29”
我使用了 XPath
//span[contains(text(),'Profile Run')]/text()[last()]
但是getAttribute("innerText")、getAttribute("innerHTML") 甚至 getText() 都不起作用。
错误:
xpath 表达式的结果 //span[contains(text(),'Profile Run')]/text()[last()] 是:[object Text]。它应该是一个元素。
【问题讨论】:
-
您是否尝试先获取元素
//span[contains(text(),'Profile Run')]并从那里轻松获取文本内容? -
@dsfq 它将整个跨度作为 Web 元素返回。我只想要“ 29”
-
我知道,这就是我要说的:获得整个跨度,然后获得最后一个孩子。容易。
-
@dsfq 你的意思是这样的 driver.findElement(By.xpath(hmap("//span[contains(text(),'Profile Run')]")).getAttribute("innerHTML "
-
类似这样的东西:
driver.findElement(By.xpath(hmap("//span[contains(text(),'Profile Run')]")).lastChild
标签: javascript selenium xpath browser-automation