0

So right now I have this code which grabs the results and it works great:

module.exports = async function grabResult(page) {
  const message = await page.$eval(
    'div > div:nth-child(2)',
    (el) => el.innerText
  );

  const username = await page.$eval(
    'child(15) .username',
    (el) => el.innerText
  );

  return { message, username };
};

console.log(result);

The above code output:

{ message: 'message text goes here', username: 'John' }

What I'm trying to do is before returning the result, to check if the 'message' contains certain words.

If the 'message' contains "http", "https" for example, then it would return empty (which is what I need):

{ message: '', username: 'John' }

If it DOESN'T contain "http", "https", then it would return the 'message' result as my original code.

{ message: 'message text goes here', username: 'John' }

I know there's a command that checks if the text contains x. I found this piece of code on the other thread:

if ((await page.waitForXPath('//*[contains(text(), "Subscription Confirmed")]',30000)) !== null) {
   chk = await page.evaluate(el => el.innerText, await page.$x('//*[contains(text(), "Subscription Confirmed")]'))
   chk = 'Success'
} else { 
   chk = 'Failed'
}

But I'm struggling to combine those two into one code.

Any help would be greatly appreciated.

I'm not a coder, hope I'm clear..

robert0
  • 435
  • 2
  • 12
  • Honestly the task is a little unclear. What string text would you like to check? Message, username? What would be different? "then it won't log" - You don't seem to log anything – Vaviloff May 01 '21 at 13:33
  • hey Vaviloff, sorry about that. I have edited my question. – robert0 May 01 '21 at 13:54

1 Answers1

3

Don't overcomplicate it, you can just write a condition to check what's stored inside message. If you have more values to compare, I can image this is what you're looking for:

function containsWords(words, word) {
    return words.filter(w => w === word).length > 0;
}

function grabResult(message, username) {
    return {
        message: containsWords(['http', 'https'], message) ? '' : message,
        username: username
    };
}

It returns:

{ message: '', username: 'pavelsaman' }

when message equals to http or https. Otherwise it returns non empty message property.

Obviously it also matters what you mean by "contains", perhaps that strict equality should not be there, perhaps you want to check if a string starts with "http" or "https". Then just adjust it accordingly.


The whole file with the exported module could look like this:

function containsWords(words, word) {
    return words.filter(w => w === word).length > 0;
}

async function grabResult(page) {
    const message = await page.$eval(
        'div > div:nth-child(2)',
        (el) => el.innerText
    );
    
    const username = await page.$eval(
        'child(15) .username',
        (el) => el.innerText
    );

    return {
        message: containsWords(['http', 'https'], message) ? '' : message,
        username: username
    };
};


module.exports = grabResult;
pavelsaman
  • 7,399
  • 1
  • 14
  • 32
  • Yes, it's seems exactly what I need. I agree, the simpler, the better. My question is, where should I insert this into my original code? I assume I just need to replace my ```return { message, username };``` with your code? I would appreciate if you could paste the full code... I'm a little confused. – robert0 May 01 '21 at 15:35
  • pavelsaman, could you tell me where to insert your code? I have tried replacing ```return { message, username };``` with your code, but it doesn't work. I want to accept and upvote your answer, as we are almost there. – robert0 May 01 '21 at 17:22
  • 2
    @robert0: I edited my answer, it should be complete now. But it's better if you take time to understand what's going on in there. – pavelsaman May 01 '21 at 18:59
  • 1
    Thanks, it works. I will just have to wait until there's a message with http, https, to fully confirm, but so far the code works without any errors. Which is is a great start. Thanks again pavelsaman, appreciate your time and work! – robert0 May 01 '21 at 19:09
  • Weird, it's still returns messages with http and https :( – robert0 May 01 '21 at 19:46
  • @robert0: you need to find out what exactly is in `message`. I can imagine it could be a complete link, than you'd need to change the condition in the filter function to something like `w.includes(word)`. – pavelsaman May 01 '21 at 19:53
  • ok, which line should I change? I have tried replacing ```containsWords(words, word)``` with your ```w.includes(word)```. Also tried replacing ```words.filter(w => w === word)``` with your ```w.includes(word)```. Both gave me an error. I guess that's not what I need to change? – robert0 May 01 '21 at 20:03
  • 1
    I made a quick experiment. Maybe this will help. I have changed ```message: containsWords(['http', 'https'], message) ? '' : message,``` to ```message: containsWords(['http', 'https'], message) ? '' : username,``` to see if it actually applies containsWords or not. The message had the "http", but it outputted ```{ message: 'John', username: 'John' }```. So it seems that either it didn't recognized the http in the message, or just executed without applying the containsWord. – robert0 May 01 '21 at 20:28
  • 1
    Got it working: https://stackoverflow.com/questions/67351618/if-contains-characters-then-x-else-y-puppeteer/67351721#67351721 . Thanks pavelsaman, your code was 95% complete. Maybe it was my fault not explaining it well. Thanks for your time. – robert0 May 01 '21 at 23:41