How do i get the text of a nested element in HTML for automation using Selenium or Protractor?

前端 未结 2 442
南旧
南旧 2021-02-11 01:57

I have below HTML code with me. I need to console log or print only the desc class text - \"Print this\" and not the spell class text in protractor or

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-11 02:13

    I used Michael's solution and embedded into my test spec without calling the function. It would still be better to use it as a separate function if the need to use is recurring. However if you want an inline solution, Here's how to do it -

    it("Get First part of text", function(){
        browser.executeScript(function () {
            var el = arguments[0], text = '';
            for (var i = 0, l = el.childNodes.length; i < l; i++)
                if (el.childNodes[i].nodeType === Element.TEXT_NODE)
                    text += el.childNodes[i].nodeValue;
            return text.trim();
        },$('.desc').getWebElement()).then(function(text){
            //use expect statements with "text" here as needed
        });
    });
    

    Hope it helps.

提交回复
热议问题