Selenium webdriver 如何获取元素的文本,但不包括子元素文本

Selenium webdriver 如何获取元素的文本,但不包括子元素文本,selenium-webdriver,jquery-selectors,webdriver,Selenium Webdriver,Jquery Selectors,Webdriver,我想获取元素的文本,但不包括其元素的文本。我尝试过getText(),但它返回的文本包含所有子元素text 在以下示例中:当我从第一个div检索文本时,它返回包含其所有子元素的文本 <div class="row”> <div class="col-lg-4 section”> <div class="col-md-12”> inseam 28 30 32 </div> &l

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

在以下示例中:当我从第一个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>


当我使用“row”类从第一个div中检索文本时,它返回包含其所有子元素的文本。

  • 发生这种情况是因为从父div中检索了文本,因此子div的所有innerHTML/文本都与之一起检索
以下是仅检索必要的innerHTML/文本的方法:

1-对于“内接缝28 30 32”

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

String text=driver.findElement(By.className(“col-md-12”)).getText()

2-对于“内接缝28 34 36”:

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


String text=driver.findElement(By.className(“col-md-13”)).getText()

并不是专门用Selenium来尝试的,但在jQuery中,您可以使用
contents()
来获取包括原始文本节点在内的所有元素,通过
nodeType
3(文本节点)进行过滤,然后首先使用
,在您的示例中:

JSFiddle:


发生这种情况是因为您正在尝试获取父标记的文本。如果你想得到某个孩子的标签,你必须一路到达那里。您可以使用“第n个子项”或“第n个类型”。例如,在本例中,如果您希望返回此文本“inseam 28 34 36”

CSS选择器将是“div.row div:nth of type(3)”,或者您可以直接指定div类“div.col-md-13”


您可以参考这篇关于选择器的更多信息的文章

我已经搜索了一段时间了,这是我为那些可以指定WebElement或WebElement列表的人提供的解决方案:

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")]')]

分享编写的代码,这将有助于更好地理解您的问题。Shoaib,我只是尝试阅读元素的文本,不包括子元素文本。List el=driver.findElements(By.xpath(“*”);对于(WebElement e:el){e.getText();}Shesh,我想阅读一个元素的文本,但不包括子元素文本。Sean,你能告诉我你实际上想从你提供的上述html代码中获取什么文本吗。在上面的代码中,div.row将返回我前面的注释中提到的所有元素的文本。此外,div.row本身没有任何文本。在上面提到的代码中,只有两个元素将返回文本:div.col-md-12=“inseam 28 30 32”和div.col-md-13=“inseam 28 34 36”。Shesh,我正在尝试读取字体,并与页面上所有元素的某些字体进行比较。在许多情况下,包含文本的元素有多个不包含任何文本的父元素,并且设置的字体与显示的文本不同。因此,我想从比较中删除没有文本的元素。但是我无法使用getText()实现这一点,因为它返回所有子元素的文本。如果这是正确的,那么这是错误的方法,您无法通过getText()获取字体值。要测试字体,必须使用getCSSValue()或使用javascript executor.Thank。我将尝试找出如何使用selenium运行此jquery代码。
[remove_child_text_from_webelement(e) for e in browser.find_elements_by_xpath('//div[contains(@class,"person")]')]