我是劇作家新手,在tumblr上導航時無法關閉彈出視窗
'cookies Policy' 和 'login' 的彈出視窗永遠不會觸發事件:'page.on("popup",', 'page.once("popup",', 'page.on("dialog",'.
我想關閉策略彈出視窗并在以下彈出視窗中登錄我的帳戶。謝謝您的幫助
我的代碼如下:
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)
uj5u.com熱心網友回復:
您可以使用單擊功能單擊按鈕,問題是按鈕位于 iframe 內。有了這個就足夠了
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/525876.html
