如何获取元素的文本但排除子元素文本

时间:2014-12-18 06:54:44

标签: selenium-webdriver jquery-selectors webdriver

我想获取元素的文本而不包含其元素的文本。我尝试过getText(),但它返回包含所有子元素文本的文本。

在以下示例中:当我从第一个div中检索文本时,它返回包含其所有子元素的文本。

<div class="row”>
    <div class="col-lg-4 section”>
        <div class="col-md-12”>
            inseam 28 30 32
        </div> 
    </div>
        <div class="col-lg-5 section”>
        <div class="col-md-13”>
            inseam 28 34 36
        </div> 
    </div>
</div>

请使用java中的webdriver告诉我如何执行此操作。

由于 肖恩

4 个答案:

答案 0 :(得分:0)

When I retrieved text from the first div with class 'row', it returns text that includes all its subelements.

  • 这是因为您从父div 检索了文本,因此子div的所有innerHTML /文本都与它们一起被检索。

以下是仅检索必要的innerHTML /文本的方法:

1- &#39; inseam 28 30 32&#39;

String text = driver.findElement(By.xpath("//div[@class='col-md-12']")).getText();

OR

String text = driver.findElement(By.className("col-md-12")).getText();

2- &#39; inseam 28 34 36&#39;

String text = driver.findElement(By.xpath("//div[@class='col-md-13']")).getText();

OR

String text = driver.findElement(By.className("col-md-13")).getText();

答案 1 :(得分:0)

没有特别使用Selenium,但使用jQuery,您可以使用contents()获取所有元素,包括原始文本节点,过滤nodeType 3(文本节点)和然后在您的示例中使用first

JSFiddle: http://jsfiddle.net/TrueBlueAussie/p33gcfk2/1/

var text = $('.row').contents().filter(function () {
    return this.nodeType == 3;
}).first();
alert(text.text());

答案 2 :(得分:0)

这种情况正在发生,因为您正在尝试获取父标记的文本。如果你想得到特定孩子的标签,你必须一直到达那里。你可以使用&#34; nth-child&#34;或&#34; nth-of-type&#34;。例如,在这种情况下,如果您想要返回此文本&#34; inseam 28 34 36&#34;。

CSS选择器将是&#34; div.row div:nth-​​of-type(3)&#34;或者你可以直接指定div类&#34; div.col-md-13&#34;

有关选择器https://saucelabs.com/resources/selenium/css-selectors

的更多信息,请参阅此文章

答案 3 :(得分:0)

我一直在寻找相同的东西,这是我为那些可以指定WebElement或WebElements列表的人的解决方案:

def remove_child_text_from_webelement(webelement):
    # Declaring the current text for this webelement
    current_text = webelement.text
    # Getting its childs elements in a list
    childs_list = webelement.find_elements_by_xpath('./*')
    # Manipulating text to remove child text from parents
    childrens_text_list = [child.text for child in childs_list]
    #return (childrens_text_list,type(childrens_text_list))
    for children_text in childrens_text_list:
        match_index = current_text.find(children_text)
        if match_index != -1:
            match_length = len(children_text)
            current_text = current_text[0:match_index] + current_text[match_index+match_length:]
    return current_text

现在您可以执行以下操作:

[remove_child_text_from_webelement(e) for e in browser.find_elements_by_xpath('//div[contains(@class,"person")]')]