Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

How to catch browser dialogs (e.g. "download or open") with Playwright for Python?

I was trying to handle a browser dialog with Playwright for Python in Firefox (in this case, but I guess the browser does not matter), i.e. a dialog that was opened by Firefox to ask whether to open or save a file. The dialog pops up after I click a button. The link to the file is not exposed, so I cannot download it in another way.

I tried catching a dialog event:

with page.expect_event("dialog") as page_info:
    button = page.querySelector('button[id="download"]')
    button.click()

which times out. I then thought I found the solution in this GitHub ticket. However, the following did not work either:

page.on("dialog", lambda dialog: dialog.accept())
page.click("button")

Do these kinds of dialog - in contrast to dialogs e.g. raised by JavaScript alert() - not trigger a dialog event? I think they may not, judging by the answers to this post: Is it possible to catch browser's File Open/Save dialog event using Javascript.

If so, how can I accept or dismiss such a dialog using python-playwright?

question from:https://stackoverflow.com/questions/65875543/how-to-catch-browser-dialogs-e-g-download-or-open-with-playwright-for-pytho

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

page.once("dialog", lambda dialog: dialog.accept())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...