How To - Accept YouTube Cookies

Hi,

I want to collect data from YouTube channesls. But I fail to accept cookies from YouTube. I suppose it has something to do with this shadow-dom magic or my lack of competence with those crazy locator strategies from playwright. I already spent two evenings without any luck, maybe one of you know how to solve it.

This is the task:

*** Settings ***
Library    RPA.Browser.Playwright

*** Keywords ***
Open YouTube Channel
    [Arguments]    ${channel}
    New Browser    headless=False
    New Page    https://www.youtube.com/channel/${channel}/featured
    
Click on videos panel
    Click    xpath=//div[@id="tabsContent"]/paper-tab[2]
    
Click on first video
    Click    xpath=//ytd-two-column-browse-results-renderer//div[@id="contents"]//div[@id="items"]//ytd-thumbnail
    
Anticipate SignIn PopUp
    Sleep    10s
    ${page_contains_sign_in}    Get Element Count    xpath=//ytd-popup-container/paper-dialog    then    ${True} if value else ${False}
    Run Keyword If    ${page_contains_sign_in}    Click    xpath=//div[@id="dismiss-button"]

Anticipate Cookie PopUp
    Sleep    5s
    ${agree_to_cookies}    Get Element Count    [id=introAgreeButton]    then    ${True} if value else ${False}
    #Run Keyword If    ${agree_to_cookies}     
    Click    [id=introAgreeButton]

*** Tasks ***
Click Picture
    Open YouTube Channel    UCmdUNLWfRA2MvXlN9c5vtNA
    Click on videos panel
    Click on first video
    Anticipate SignIn PopUp
    Anticipate Cookie PopUp    # this is where I cannot find the "I agree" button

While somehow the SignIn Popup is no problem (although i wonder why my randomly build locator works), I fail to click on I Agree in the Cookie PopUp .

Regards,
Markus

Hi Markus!

I think the problem you’re having is the fact that the cookie consent dialog Youtube creates is inside an iframe. Playwright pierces shadow DOMs automatically, but you will have to explicitly go inside iframes to match elements.

There’s a description in the “Frames” section of the keyword docs: https://marketsquare.github.io/robotframework-browser/Browser.html#Finding%20elements

With CSS selectors accepting the two dialogs would look like this:

Accept SignIn PopUp
    Click    \#dismiss-button

Accept Cookie PopUp
    Click    \#iframe >>> \#introAgreeButton

The important part being the >>> operator for going inside frames.

As a side-note, Playwright does internal waiting so in most cases sleeps and other tricks required with Selenium won’t be needed. Of course my example won’t work when the dialog doesn’t spawn, but it should be doable “waitless”. I’ll have to ask one of our Playwright experts on what the ideal approach would be for that, after the new year.

3 Likes