Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
混合内容html span的selenium xpath刮取_Html_Selenium_Selenium Webdriver_Xpath - Fatal编程技术网

混合内容html span的selenium xpath刮取

混合内容html span的selenium xpath刮取,html,selenium,selenium-webdriver,xpath,Html,Selenium,Selenium Webdriver,Xpath,我正在尝试删除一个包含混合内容的span元素 <span id="span-id"> <!--starts with some whitespace--> <b>bold title</b> <br/> text here that I want to grab.... </span> 我尝试将/text()添加到表达式中,该表达式也不会返回任何结果。如果我添加/b,我会得到粗体文本的文本内容,而这恰好是我

我正在尝试删除一个包含混合内容的span元素

<span id="span-id">
  <!--starts with some whitespace-->
  <b>bold title</b>
  <br/>
  text here that I want to grab....
</span>
我尝试将/text()添加到表达式中,该表达式也不会返回任何结果。如果我添加/b,我会得到粗体文本的文本内容,而这恰好是我不感兴趣的标题


我相信有了一点xpath的魔力,这应该很容易,但到目前为止我还没有找到它!!还是有更好的办法?非常感谢您的评论。

我相信下面的xpath查询应该适用于您的案例。下面的兄弟姐妹对您尝试做的事情很有用

//span[@id='span-id']/br/following-sibling::text()
我尝试将
/text()
添加到表达式中,该表达式也不返回任何内容

这将选择上下文节点的所有文本节点子节点,其中有三个子节点

您所指的“nothing”很可能是其中的第一个,它是一个只有空白的文本节点(因此您可以在其中看到“nothing”)

您需要的是

//span[@id='span-id']/text()[3] 
//span[@id='span-id']/text()[last()] 
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*">
     "<xsl:copy-of select="//span[@id='span-id']/text()[3]"/>"
 </xsl:template>

</xsl:stylesheet>
     "
    text here that I want to grab....   
"
当然,还有其他可能的变化

//span[@id='span-id']/text()[3] 
//span[@id='span-id']/text()[last()] 
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*">
     "<xsl:copy-of select="//span[@id='span-id']/text()[3]"/>"
 </xsl:template>

</xsl:stylesheet>
     "
    text here that I want to grab....   
"
或:

基于XSLT的验证

//span[@id='span-id']/text()[3] 
//span[@id='span-id']/text()[last()] 
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*">
     "<xsl:copy-of select="//span[@id='span-id']/text()[3]"/>"
 </xsl:template>

</xsl:stylesheet>
     "
    text here that I want to grab....   
"

好问题,+1。请参阅我的答案以了解解释和三个可选的XPath表达式,每个表达式都精确地选择所需的文本节点。还提供了基于XSLT的验证。我使用xml/xsl文件尝试了所有这些方法,所有这些方法都很有效。有趣的是,Selenium在使用其中任何一种时都不起作用:(但我确实发现了,我知道这是一种黑客行为,但使用//span[@id='span-id']]/b/。填充span的webelement.text确实有效(诚然,span下的所有文本都会返回,但我可以使用它!)再次感谢。@SGB:不客气。我很遗憾Selenium作为XPath处理器没有完全兼容——我会记住你的诀窍。