Chrome controlled by Chromedriver silenced alert popup

I used the following html to test an alert pop up. If a user fills the form, but then switches URL, instead of submitting the form, the page will pop up an alert

<!DOCTYPE html>
<html>

<body>
    <div class="container">
        <form action="#">
            <label>First Name</label>
            <input type="text" id="fname" name="firstname" onchange="updateFname()">
            <button class="submit-btn" onclick="return false;">
                Submit
            </button>
        </form>
    </div>
    <script type="text/javascript">
        let fname = '';
        updateFname = () => {
            fname = document
                .getElementById('fname').value;
        }
        window.addEventListener('beforeunload',
            function (e) {
                if (fname !== '') {
                    console.log("form is filled but not submitted yet");
                    e.preventDefault();
                    e.returnValue = '';
                }
            });
    </script>
</body>

</html>

When I tested the page in chrome alone, the alert popped up as expected
chrome alone

But when I run Selenium with ChromeDriver and Chrome, Chrome didn’t pop up the alert and switched the URL directly.

chrome under chromedriver

The old Chrome and Chromedriver (version under 100 worked fine. Alert popped up).
My Chrome and Chromedirver version is 131.

Is there a way to make the alert pop up again?

Thank you