How to handle website notices if they appear

Most websites display a notice when you visit them the first time (cookies, privacy, newsletter subscription, other agreements). If your robot includes visiting a website, often dismissing the notices will be the first task you need to tell your robot to do.

Something you need to consider is that sometimes there is a slight delay before the notice is displayed. On some occasions, the notice might not display at all!

The following robot will click on an element if it appears. If not, the robot will ignore it:

*** Settings ***
Documentation     Click an element if it appears. Ignore if not!
Library           RPA.Browser
Library           RPA.RobotLogListener

*** Variables ***
${THIS_LOCATOR_MATCHES}=    css:input
${THIS_LOCATOR_DOES_NOT_MATCH}=    this-locator-does-not-match-anything

*** Keywords ***
Open The Website
    Open Available Browser    https://robocorp.com/docs/

*** Keywords ***
Click Element If It Appears
    [Arguments]    ${locator}
    Mute Run On Failure    Click Element When Visible
    Run Keyword And Ignore Error    Click Element When Visible    ${locator}

*** Tasks ***
Click the element if it appears. Ignore if not!
    Open The Website
    Click Element If It Appears    ${THIS_LOCATOR_MATCHES}
    Click Element If It Appears    ${THIS_LOCATOR_DOES_NOT_MATCH}
    [Teardown]    Close All Browsers

The wait-and-click-or-ignore logic is implemented in the custom Click Element If It Appears keyword:

*** Keywords ***
Click Element If It Appears
    [Arguments]    ${locator}
    Mute Run On Failure    Click Element When Visible
    Run Keyword And Ignore Error    Click Element When Visible    ${locator}

The default logging behavior is to embed a screenshot in the log when a browser keyword fails. In this case, we don't want the screenshot (the screenshots might be big and take unnecessary cloud storage space). We accept the occasional failure as a normal condition (sometimes the notice is there, sometimes not 🤷‍♀️).

The Mute Run On Failure keyword (from the RPA.RobotLogListener library) prevents the Click Element When Visible keyword (from the RPA.Browser library) from generating a screenshot on failure.

The Click Element When Visible keyword waits for the element to become visible before clicking on it. If the element does not appear, the error caused by the non-matching locator is ignored by the Run Keyword And Ignore Error keyword (Robot Framework built-in library):

With those notices out of the way, your robot can start interacting with the website!