1

I am new to playwright and I'm having trouble to close popup windows when navigating on tumblr

The popup windows for 'cookies Policy' and 'login' never trigger the events: 'page.on("popup",', 'page.once("popup",', 'page.on("dialog",'.

I would like to close the policy popup and login to my account on the following popup. Thanks for the help

My code is as follow:

from time import sleep

from playwright.sync_api import sync_playwright


def close_popups(page):
    print("popup event")
    close_button = page.locator("// button[@class='components-button white-space-normal is-primary']")
    close_button.click()


with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page_context = browser.new_context()
    page = page_context.new_page()

    page.on("popup", close_popups)
    page.once("popup", close_popups)

    page.on("dialog",
            lambda dialog: print("test")
            # lambda dialog: dialog.accept()
            )
    page.goto("https://www.tumblr.com/search/handbags?t=365")

    page.wait_for_load_state(state="domcontentloaded")

    print(page.title())

    # scroll down
    # https://stackoverflow.com/questions/69183922/playwright-auto-scroll-to-bottom-of-infinite-scroll-page
    for i in range(5):  # make the range as long as needed
        page.mouse.wheel(0, 15000)
        sleep(2)
        i += 1
    # browser.close()
    sleep(2000)

Je Je
  • 508
  • 2
  • 8
  • 23

1 Answers1

1

You can click into the button with click function, problem is that the button is inside an iframe. With this should be enough

from time import sleep
from playwright.sync_api import sync_playwright

def do_with_frame_attached(frame):
    # This code will be executed for every iframe is attached (Appear) on the page
    print(f"I am the frame detached:{frame}")
    if frame.is_visible(".is-primary"):
        frame.click(".is-primary")

def do_with_frame_detached(frame):
    # This code will be executed for every iframe is detached (Disappear) from the page
    print(f"I am the frame detached:{frame}")



with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page_context = browser.new_context()
    page = page_context.new_page()
    page.on("frameattached", do_with_frame_attached)
    page.on("framedetached", do_with_frame_detached)
    page.goto("https://www.tumblr.com/search/handbags?t=365")
    page.wait_for_load_state(state="domcontentloaded")

    print(page.title())

    # scroll down
    # https://stackoverflow.com/questions/69183922/playwright-auto-scroll-to-bottom-of-infinite-scroll-page
    for i in range(5):  # make the range as long as needed
        page.mouse.wheel(0, 15000)
        sleep(2)
        i += 1
    # browser.close()
    sleep(2)
Jaky Ruby
  • 1,409
  • 1
  • 7
  • 12
  • tx yeah I just realised the iframe, is there a way to monitor which iframe is active with an event, as this doesn't always show up. or you think forcing focus will always work – Je Je Oct 31 '22 at 18:27
  • I would say that specific pop up will always appear. Anyways, you can cheeck if the iframe is visible before make the click. I update the answer – Jaky Ruby Oct 31 '22 at 18:34
  • have accepted as anwser but would have preffered an event driven solution as these iframes comes up at any time and can make the code bug, unless everything is in a try bracket.. tx – Je Je Oct 31 '22 at 19:44
  • 1
    @JeJe Sorry, I did not understand what you said. I got it. I edited my answer. Basically the event `page.on("frameattached", do_with_frame)` with a callback – Jaky Ruby Oct 31 '22 at 21:52
  • So I get an error with the "detached event", i guess i need to register which frame I want to get back? Tx for your help. very much apreciated – Je Je Nov 01 '22 at 21:25
  • 2
    Updated: Problem of those iframes is that they do not have name or url. There is no way you can identify them except checking the elements they have inside. And you can only do that with the event `frameattached`, but you can't do it with the event `framedetached` because it is closed, and there is no element to manage. – Jaky Ruby Nov 01 '22 at 22:37