How to get text only from the parent element excluding child using protractor xpath function?

Related searches

I am trying to get the text of the parent element alone that has one or more child. How do I get it using XPath?

(I am using the above XPath as an element identifier with the getText() method of the protractor)

The text of the parent element changes based on the user language selection, so I preferred to use the id attribute to identify the element

*<div class="header" id="Welcome_Label">
    "Welcome to the Mail page"
  <button class="btn" id="open">
    Open
    </button>
</div>*

xpath tried to get the parent text is

//div[@id='Welcome_Label']/*[not(self::button)]

I am expecting it to return the text parent text "Welcome to the Mail page", but it's returning "Welcome to the Mail page Open".

Unless I'm missing something, using just your code

//div/text()

Outputs:

"Welcome to the Mail page"

Is that what you're looking for?

Exclude text content of child elements of the parent element in , Exclude text content of child elements of the parent in Selenium WebDriver with help of jQuery. I want to get the text of the parent tag only, that is “Google.co.in offered in: $("div#_eEe").contents().filter(function(){ return this. Also introduces the possibility to include text from certain child elements whilst excluding from all others. In case all elements are empty, an empty string is returned. This method is slower than the previous one because of the number of method calls and work performed by each method.

Welcome to SO.

If you are using the method that will get the text from only parent node.

Python Implementation:

def get_text_exclude_children(element):
return driver.execute_script(
    """
    var parent = arguments[0];
    var child = parent.firstChild;
    var textValue = "";
    while(child) {
        if (child.nodeType === Node.TEXT_NODE)
                textValue += child.textContent;
                child = child.nextSibling;
    }
    return textValue;""",
    element).strip()

In you case you can call this as below.

ele = driver.find_element_by_css_selector(".header")
value = get_text_exclude_children(ele)
print (value)

Java Implementation:

public String get_text_exclude_children(WebDriver driver, WebElement element) {
    JavascriptExecutor js = (JavascriptExecutor) driver;  
    return (String) js.executeScript("var parent = arguments[0];"
                                    + "var child = parent.firstChild;"
                                    + " var textValue = ''; while(child) { "
                                            + "if (child.nodeType === Node.TEXT_NODE)"
                                                + " textValue += child.textContent;"
                                                + " child = child.nextSibling; "
                                                + "} return textValue;",element);

XPath in Protractor, Xpath in Protractor, Xpath finds the element(s) along with protractor, and other xpath, xpath based on attribute, xpath based on text, contains functions in xpath, dot When we have a matching element only under one parent(this case), we has 'div' as tagname and id as fruit, look for immediate child(/) node which has� In the bottom of this page it has supported languages, those are maintained in a parent div tag with child tags. I want to get the text of the parent tag only, that is “Google.co.in offered in:” If we write the locator for the parent div tag and then get the text with WebDriver Api getText(),

You can try

//div[@id='Welcome_Label']/text()[../*]

It selects all text() nodes at the div level if it has at least one element child.

Xpath in Protractor, Protractor : Xpath finds the element(s) along with protractor, and other automation tools. xpath, xpath based on attribute, xpath based on text, contains functions in xpath except tagname is attribute, any number of attributes can present in html code When we have matching element only under one parent(this case), we� A Sibling in Selenium Webdriver is a function used to fetch a web element which is a sibling to the parent element. If the parent element is known then the web element can be easily found or located that can use the sibling attribute of the Xpath expression in selenium webdriver.

I had a similar issue and was found the following:: http://exploreselenium.com/selenium/exclude-text-content-of-child-elements-of-the-parent-element-in-selenium-webdriver/

Using that, I just added the following function, although in this case I'm using CSS instead of XPath.

async function getTextExcludingChildren(css: string) {
    return await browser.executeScript(`return $("${css}").clone().children().remove().end().text();`);
}

Protractor, I've been using following-sibling in xpath but no use as it is getting the sibling of the How to exclude child nodes with XPath selector? contain certain a elements only if the div elements already exist in the input document; So basically I want to get /dialog/id/7 if in any of elements children has text Text_need_to_find Testing Xpath test bed. Test queries in the Xpath test bed: Xpath test bed (whitebeam.org); Browser console $x("//div") Works in Firefox and Chrome. Selectors

How to find a specific child element with Protractor, How to find a specific child element with Protractor - css-selectors. Try this out this may help you element.all(by.locator(element)).filter(function(el,index){ return el. Selenium - Identifying an HTML element without an ID or CSS class name result of your first XPath to return only div that has inner text or has attribute style. get the parent element's text, for each child, get the text and remove it from the parent's text. What you would have left is the desired text - Google Link in your case. if you want to get the Google Link just to make an assertion, it could be that you would be okay with checking if the parent's text s tarts with Google Link.

XPath Introduction XPath Nodes XPath Syntax XPath Axes XPath Operators XPath Examples XSLT Tutorial XSLT Introduction XSL Languages XSLT Transform XSLT <template> XSLT <value-of> XSLT <for-each> XSLT <sort> XSLT <if> XSLT <choose> XSLT Apply XSLT on the Client XSLT on the Server XSLT Edit XML XSLT Examples

xpath not working in protractor.I ahve cretaed xpath using firebug.When I identify the xpath using IDE,it is working fine.However when I use the same xpath in protractor,it is not working.My element does not have id or name.So here i can use only xpath option. Please find the below image for reference.

Comments
  • "Welcome to the Mail page" is part of the first text node child of div element. But *[not(self::button)] will select just elements.
  • Thank you for the reply, but I use this XPath inside a xpath function of protractor which looks something like let text = element(by.xpath("//div/text()")).getText(), but when I use during runtime it throws an error Failed: invalid selector: The result of the xpath expression "//div/text()" is: [object Text]. It should be an element.
  • Understood; you may want to edit your question to clarify that and add anything else that may be useful for user trying to help.
  • Thank you for the reply, but I use this XPath inside a xpath function of protractor which looks something like let text = element(by.xpath("//div[@id='Welcome_Label']/text()[../*]")).getText(), but when I use during runtime it throws an error Failed: invalid selector: The result of the xpath expression "//div/text()" is: [object Text]. It should be an element. –