简体   繁体   中英

Selenium Protractor in Javascript: Extracting getText keeps returning undefined

I have an element with inner text on my page I want to extract.

So I do:

element(by.xpath('//*[@id="searchQuery"]')).getText().then(function (text){
    return text;
});

but I always get an undefined return value.

Is there another way of extracting text?

What you can do is to wait for a particular text to be present in element :

var EC = protractor.ExpectedConditions;
var searchQuery = $('#searchQuery');

var condition = EC.textToBePresentInElement(searchQuery, 'some text');
browser.wait(condition, 10000, "Text is still not present");

searchQuery.getText().then(function (text) {
    console.log(text);
});

Or, if you don't know what text would be present in the element - just check if any text is there:

var EC = protractor.ExpectedConditions;
var searchQuery = $('#searchQuery');

function waitForText(elementFinder) {
    return function () {
        return elementFinder.getText().then(function(text) {
            return text !== "";  // could also be replaced with "return !!text;"
        });
    };
};

browser.wait(waitForText(searchQuery), 10000, "Text is still not present");

searchQuery.getText().then(function (text) {
    console.log(text);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM