Warm tip: This article is reproduced from serverfault.com, please click

If .textContent contains X then click

发布于 2020-11-29 23:30:06

I'm looking to click on every email in my gmail inbox (first page inbox) that contains/starts with the word "Security" (e.g it will click on the below email)

<span id=":31" class="bog">
<span class="bqe"">
Security alert - please check here
</span>
</span>
</pre>

There are 50 .bog elements (the subject lines of the email preview) in my inbox.

I want to find which of those 50 elements contain "Security", and click on them. It may contain other words.

I have tried:

const elements = await page.$$(".bog");
var text = await page.evaluate(() => document.querySelector('.bog').textContent);
console.log(text);
if (text.textContent === "Security") {
    text.click();
} else {
    console.log("NO")
}
    const elements = await page.$$(".bog");
    const emails = Array.from(elements);
    emails.forEach((email) => {
        if (email.textContent === 'Security alert') {
           email.click();
           console.log("found");
        }
    })

await page.$$eval('.bog', elements => {
    const element = elements.find(element => element.innerHTML === 'Security alert');
    element.click();
});
emails.forEach((email) => {
   if (email.textContent === 'Security alert') {
      email.click();
      console.log("found");
   }
    console.log(email.textContent)
})

But i really don't know what to do - would appreciate some guidance. All of them gave me varying errors, but i notice when i try to do if X.textContent i often get an undefined error?

Let me know if i should give any more information.

Thanks!

Questioner
Dave
Viewed
11
10.4k 2020-12-09 07:08:34

When you use document.querySelector(".cssSelector") you grab the first element that correspond to that css selector, you are not grabbing all the elements. To grab all the elements you could use document.querySelectorAll(), which returns a NodeList (Array-Like) list of all the elements.

Then, in order to search for a specific substring in your element.textElement, you can use various string built-in methods or even regular expressions. Yuo can use for loops, forEach, Array.prototype.filter, the sky here is the limit.

Since your requirements are not very clear about the index position of the substring "Security" (words that begin with it, middle of the word, where?), you could use String.prototype.indexOf(), String.prototype.includes and so forth.

Use case:

const nodeBogs = document.querySelectorAll(".bogs");
nodeBogs.forEach(elem => {
  if (elem.textContent.includes("Security") {
  // do your things here
});

You can also use Array.prototype.filter:

const nodeBogs = document.querySelectorAll(".bogs");
const filteredElems = [...nodeBogs].filter(elem => elem.textContent.includes("Security"));

// now you have an Array of filtered elements that contain the word "Security"

These use cases are really the tip of the iceberg. You can tackle this simple problem in a variety of different ways. Check out the Mozilla documentation, get familiar with JS built-in methods and with the syntax of the language. Then, you are at the beginning of your journey, test your program as often as you can. Use console.logs, a text editor built-in debugger, anything that will help you understand where you error or how your code behaves.