How to close a modal or cookie dialog

While automating web interactions, it's common for a modal or cookie dialog to interrupt your script. Fortunately, you can close them with a script of your own.

Overview

This guide shows you how to close modals and cookie dialogs using AgentQL.

Locate the reject button

The key to closing a modal or cookie dialog is to identify the close/reject button associated with it. You can use the query_elements() method to locate the button like so:

close_modals.py
python
QUERY = """
{
    cookies_form {
        reject_btn
    }
}
"""

response = page.query_elements(QUERY)

Click the reject button

Once you have located the reject button, you can click it to close the modal or cookie dialog.

close_modals.py
python
response.cookies_form.reject_btn.click()

Put it all together

If you want to take a look at how everything comes together in a single script, it's available in our Github examples repo here