0

the user will provide a specific email address. in this case, varEmail = "admin@domain-name.onmicrosoft.com". now I have to check this on the web page list programmatically. I should be able to return the exact match of the user defined email address on the web page list.

Currently, I have these (2) email addresses on my trial environment.enter image description here

I am also using this Javascript to get one of the email in the web page list.

(Array.from(document.getElementsByClassName("ms-List-surface")[2].getElementsByClassName("ms-List-cell")).filter(name => name.textContent.match(/admin@domain-name.onmicrosoft.com/)))

how ever I am getting 2 output instead of the exact user define email address. Output: psi-admi@domain-name.onmicrosoft.com admin@domain-name.onmicrosoft.com

enter image description here

I am hoping that there is someone here will noticed my post and help me on this. thank you all in advanced!


Just an update, Here's what I've tried so far.

Array.from(document.getElementsByClassName("ms-List-surface")[2].getElementsByClassName("ms-List-cell")).filter(acct => acct.textContent === 'admin@domain-name.onmicrosoft.com');

Return Empty Empty Result

By the way, below is the screenshot of the page from where I need to get the Email/Account. Compliance New Content Search

  • 1
    If you need an exact match you can just compare strings directly instead of using a regex. – tromgy Nov 02 '22 at 14:56
  • @tromgy thank you for your response, I'm not sure how will I able to implement this on my current JS. code may I ask if you can give me more details about this. or show me a snippet. thank you very much! – euphoria05 Nov 02 '22 at 15:12
  • I've also tried to add ^ and $ but it doesn't give me output – euphoria05 Nov 02 '22 at 15:53
  • 1
    You could use something like `filter(name => name.textContent === 'admin@domain-name.onmicrosoft.com')`. To make it more robust you can also add `toLowerCase()` before comparing, but if you're sure `textContent` is always in lower case that can be omitted. – tromgy Nov 03 '22 at 00:08
  • @tromgy Good day! I posted an update, with screenshot. I've tried to use 'Strict equality' as you've suggested, but It returns empty result. Again!, thank you very much for helping me out on this. – euphoria05 Nov 03 '22 at 18:09

2 Answers2

0

I'm guessing the data in your HTML has some garbage in there, that's why exact match won't find what you need.

Try to clean the string up before comparing it, something like:

filter(name => name.textContent.trim().toLowerCase() === 'admin@domain-name.onmicrosoft.com')

that way you remove all the white spaces at the beginning and the end of the string, and convert it to lowercase (just in case someone messed up)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/tolowercase

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
0

This inquiry has been resolved. Thanks for the help of my Manager, who figured this all out. Again, I've just witnessed a cool pro dev's work ethic, from Identifying the where and how, the right search keywords, and so on.

So, yay! New feed, new learning!

here's the line of code he shared with me.

const userRow = document.evaluate("//div[text()='admin@domain-name.onmicrosoft.com']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.offsetParent;

He checked and Analyzed the result from the console. Since I am using 'textContent' or 'innerText', We went there to see its value. We found that both had an excess string attached to them ( e.g. usernameadmin@domain-name.onmicrosoft.com ), that's why the above-suggested solutions didn't work (By the way, I would like to thank @Facundo & @tromgy).

From there, He finds ways to work on it and come up with a solution.

Please see link below for your reference:

How to get element by innerText

HTMLElement.offsetParent

Introduction to using XPath in JavaScript