microsoft / playwright-python

Python version of the Playwright testing and automation library.
https://playwright.dev/python/
Apache License 2.0
11.51k stars 876 forks source link

[BUG]FileChooser does not work. #1530

Closed rffanlab closed 2 years ago

rffanlab commented 2 years ago

Context:

Code Snippet

Help us help you! Put down a short code snippet that illustrates your bug and that we can run and debug locally.

        with sync_playwright() as p:
            self.browser = p.chromium.launch_persistent_context(user_data_dir=root_path+"/components/chromedata",headless=self.headless,ignore_default_args=["--inPrivate"])
            # self.context = self.browser.new_context(
            #     user_agent=USER_AGENT,accept_downloads=True,locale="zh-CN"
            # )
            self.page = self.browser.new_page()
            self.page.goto(url=pageUrl)
            for item in actionList:
                if "sleep" in item:
                    time.sleep(item["sleep"])
                self.page.wait_for_selector(item["selector"])
                pageElement = self.page.locator(item["selector"])
                if "click" == item["action"]:
                    pageElement.click()
                if "filechooser" == item["action"]:
                    pageElement.click()
                    # self.page.pause()
                    # 无法监控到爱奇艺的上传窗口。
                    self.page.on("filechooser", lambda file_chooser: file_chooser.set_files("/Users/fangluyu/tmp/Waves-70796.mp4"))

            self.page.close()
            self.browser.close()
186347353-41c7f85d-d352-4f6b-8c2d-4fcdcd5f5dce

When start Using page.on("filechooser") it seems does not work for me. It failed to set file to filechooser

rffanlab commented 2 years ago

Same as Firefox.

rffanlab commented 2 years ago

I Debuged get into "setFileChooserInterceptedNoReply "

rffanlab commented 2 years ago

I change into Java got failed like this Exception in thread "main" java.lang.NullPointerException: Cannot read field "isRemote" because the return value of "com.microsoft.playwright.impl.BrowserContextImpl.browser()" is null at com.microsoft.playwright.impl.Utils.addLargeFileUploadParams(Utils.java:168) at com.microsoft.playwright.impl.ElementHandleImpl.setInputFilesImpl(ElementHandleImpl.java:472) at com.microsoft.playwright.impl.FileChooserImpl.lambda$setFiles$0(FileChooserImpl.java:62) at com.microsoft.playwright.impl.LoggingSupport.lambda$withLogging$0(LoggingSupport.java:36) at com.microsoft.playwright.impl.LoggingSupport.withLogging(LoggingSupport.java:47) at com.microsoft.playwright.impl.ChannelOwner.withLogging(ChannelOwner.java:85) at com.microsoft.playwright.impl.LoggingSupport.withLogging(LoggingSupport.java:35) at com.microsoft.playwright.impl.FileChooserImpl.setFiles(FileChooserImpl.java:61) at com.microsoft.playwright.impl.FileChooserImpl.setFiles(FileChooserImpl.java:56) at com.microsoft.playwright.FileChooser.setFiles(FileChooser.java:80) at io.afu.autodeployweb.components.Test.main(Test.java:30)

rwoll commented 2 years ago
from playwright.sync_api import sync_playwright, expect
import tempfile

with sync_playwright() as p:
    with tempfile.TemporaryDirectory() as tmp_dir:
      browser = p.chromium.launch_persistent_context(user_data_dir=tmp_dir, headless=False)
      page = browser.new_page()
      # https://playwright.dev/python/docs/api/class-filechooser
      page.goto("https://pw.rwoll.dev/file-uploads")
      with page.expect_file_chooser() as fc_info:
          page.locator("#file-uploader").click()
      file_chooser = fc_info.value
      file_chooser.set_files("foo.txt")
      expect(page.locator("body")).to_contain_text("Name: foo.txt, Size: 7")
      page.pause() # pausing test so user can manually look for demo purposes
      browser.close()

The above is working for me, assuming I have foo.txt in the directory next to my script. Can you create a small repro by modifying the above showing it break in the same way you are seeing?

NB: self.page.on("filechooser", lambda file_chooser: file_chooser.set_files("/Users/fangluyu/tmp/Waves-70796.mp4")) as the last line in your script looks like it would be flaky. You'll want to start listening for the event before you take the action that can trigger it.

joseproig commented 2 years ago

My context:

Playwright Version: 1.25.2 Operating System: win10 Python version: 3.10 Browser: Chromium

In Safari and Firefox is working for me, but in Chromium it clicks to the input but it does not appear the filechooser and appears me this error: playwright._impl._api_types.TimeoutError: Timeout 30000.0ms exceeded while waiting for event "filechooser" E =========================== logs =========================== E waiting for event "filechooser" E ============================================================

mxschmitt commented 2 years ago

Can you share a repro with us which we can run locally? Otherwise we can't act on it.