2015-09-23 66 views
2

我想从Selenium和Javascript中的HTML元素中提取一些文本。从内部元素的文本中获取文本

<span class="myAttribute"> 
    <b>SomeValueToIgnore</b> 
    <span class="ignoreWhatsInside"> 
    <em>ignore_that</em> 
    </span> 
    GiveMeJustThat_NothingElse 
</span> 

我怎样才能提取span元素在Javascript /硒文本 'GiveMeJustThat_NothingElse'? 不幸的是,我不能把文字'GiveMe ...'放到自己的标签中,因为那样会打破其他测试。

+0

为什么不能使用xpath? –

+0

此帖可能有帮助 - http://stackoverflow.com/questions/32479422/how-do-i-get-the-text-of-a-nested-element-in-html-for-automation-using-selenium。谢谢 –

+0

@Mahsum:xpath会很好。 – Bernie

回答

2

它很容易让你使用jQuery的JavaScript值。

var text = $(".myAttribute") 
    .clone()  
    .children() 
    .remove() 
    .end() 
    .text(); 

    console.log('text = ' + text); 
2

这是错误的方式,但你也可以这样做。

$(".myAttribute").text().split(" ")[$(".myAttribute").text().split(" ").length -1] 
1

对于Java:
driver.findElement(by.xpath( '//跨度[@类= “myAttribute”]'))的getText();