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!

Selenium (RPA.Browser.Selenium)

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.Selenium
Library             RPA.RobotLogListener


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


*** 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


*** Keywords ***
Open the website
    Open Available Browser    https://robocorp.com/docs/

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 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.Selenium 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!

Playwright (RPA.Browser.Playwright)

*** Settings ***
Documentation       Click an element if it appears. Ignore if not!

Library             RPA.Browser.Playwright
Library             RPA.RobotLogListener


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


*** 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}


*** Keywords ***
Open the website
    New Page    https://robocorp.com/docs/

Click element if it appears
    [Arguments]    ${locator}
    ${default_failure_keyword}=    Register Keyword To Run On Failure    ${None}
    Run Keyword And Ignore Error    Click    ${locator}
    Register Keyword To Run On Failure    ${default_failure_keyword}

Learn more about the libraries mentioned on this page:

May 5, 2022