I have found a code that worked really well for me. It involves getting the quantity of messages first and then removing them from the DOM.
public static void RemoveMessages()
{
// Removing messages from the DOM using JavaScript
int MessagesCount = Program.ChromeDriver.FindElementsByClassName("GLS-JUXDKAD").Count;
Console.WriteLine("Total messages: " + MessagesCount);
while (MessagesCount != 0)
{
MessagesCount--;
{
try
{
Program.js.ExecuteScript("var element = document.querySelector('.GLS-JUXDKAD'); element.parentNode.removeChild(element); "); // This code removes the entire message
Console.WriteLine("JavaScript execution command: remove message on the page. Total messages left: " + MessagesCount);
}
catch
{
Console.WriteLine("JavaScript error. Unable to remove message on the page. No more messages left. Total messages: " + MessagesCount);
}
}
}
}