Playwright Chromium环境下带缩略图MP4无法上传问题求助
Playwright Chromium 上传带缩略图MP4文件失败问题
问题现象
- 使用Playwright的Chromium浏览器上传MP4时,带缩略图的MP4无法上传甚至无法在界面加载,但无缩略图的MP4可正常上传;
- 改为MP3格式可正常完成上传;
- Firefox和正式版Chrome浏览器能正常上传该MP4文件。
主文件代码
from playwright.sync_api import sync_playwright from playwright.sync_api import expect from PageObjectModel.Pages.UserLogin import Login from PageObjectModel.Pages.VideosFilesUpload import VideosFilesUpload from constant import STAGING import json import os expect.set_options(timeout=1800000) def main(playwright): user_data_dir = './prefs' pref_file_path = user_data_dir + '/Default/Preferences' os.makedirs(os.path.dirname(pref_file_path), exist_ok=True) if not os.path.exists(pref_file_path): with open(pref_file_path, 'w') as pref_file: json.dump({}, pref_file) with open(pref_file_path, 'r') as pref_file: try: data = json.load(pref_file) except json.JSONDecodeError: data = {} # Initialize the data dictionary data['devtools'] = { 'preferences': { 'network_log.preserve-log': True, 'network_log.filter': 'Fetch/XHR' } } with open(pref_file_path, 'w') as pref_file: json.dump(data, pref_file) browser = playwright.chromium.launch_persistent_context( user_data_dir, headless=False, no_viewport=True, args=[ '--auto-open-devtools-for-tabs', '--start-maximized' ] ) page = browser.new_page() page.goto(STAGING) #input the url! Login(page) VideosFilesUpload(page) print("ENTIRE TEST CASE IS COMPLETED") with sync_playwright() as playwright: main(playwright)
VideosFilesUpload类代码
from playwright.sync_api import expect import time expect.set_options(timeout=1800000) class VideosFilesUpload(): def __init__(self,page): self.page = page self.final_videos_files_upload_execution() def homepage_click_attachment_icon(self): open_attachment = self.page.locator("#attachments-button-home") expect(open_attachment).to_be_visible(timeout=1800000) expect(open_attachment).to_be_enabled(timeout=1800000) open_attachment.focus() open_attachment.click(timeout=1800000) def navigate_to_transcoder(self): click_to_redirect_transcoder = self.page.locator("#upload-video-button-home") expect(click_to_redirect_transcoder).to_be_visible(timeout=1800000) expect(click_to_redirect_transcoder).to_be_enabled(timeout=1800000) expect(click_to_redirect_transcoder).to_have_text("Video/Audio" , timeout=1800000) click_to_redirect_transcoder.focus() click_to_redirect_transcoder.click(timeout=1800000) def upload_videos_files_process(self): upload_process = self.page.locator("#transcoder-input-file-upload") upload_process.set_input_files("C:\\Users\\Jerome\\Desktop\\Benchmarking\Markets in 3 Minutes_ The Pound's World-Beating Run 3.13.mp4") expect(upload_process).to_be_attached(timeout=1800000) def transcoder_click_upload_button(self): click_upload_button = self.page.locator("#video-upload-btn-video-file") expect(click_upload_button).to_be_visible(timeout=1800000) expect(click_upload_button).to_be_enabled(timeout=1800000) expect(click_upload_button).to_have_text("Upload" , timeout=1800000) click_upload_button.focus() click_upload_button.click(timeout=1800000) def final_videos_files_upload_execution(self): self.homepage_click_attachment_icon() self.navigate_to_transcoder() self.upload_videos_files_process() self.transcoder_click_upload_button() print("test videos completed".upper().strip())
解决方法
1. 禁用Chromium媒体缩略图生成
Playwright内置的Chromium版本可能对MP4嵌入缩略图的解析存在兼容性问题,添加命令行参数关闭缩略图生成:
修改主文件中launch_persistent_context的args参数:
browser = playwright.chromium.launch_persistent_context( user_data_dir, headless=False, no_viewport=True, args=[ '--auto-open-devtools-for-tabs', '--start-maximized', '--disable-media-thumbnail-generation' # 新增参数 ] )
2. 修复文件路径转义错误
代码中路径的\M会被解析为转义字符,改用原始字符串避免问题:
修改upload_videos_files_process方法中的路径:
upload_process.set_input_files(r"C:\Users\Jerome\Desktop\Benchmarking\Markets in 3 Minutes_ The Pound's World-Beating Run 3.13.mp4")
3. 模拟真实用户文件选择流程
替代直接调用set_input_files,模拟点击上传按钮后选择文件的真实流程:
def upload_videos_files_process(self): # 触发文件选择框 self.page.locator("#transcoder-input-file-upload").click() # 等待并选择文件 with self.page.expect_file_chooser() as fc_info: file_chooser = fc_info.value file_chooser.set_files(r"C:\Users\Jerome\Desktop\Benchmarking\Markets in 3 Minutes_ The Pound's World-Beating Run 3.13.mp4") # 可选:验证文件是否被页面识别 expect(self.page.locator("#uploaded-file-name")).to_have_text("Markets in 3 Minutes_ The Pound's World-Beating Run 3.13.mp4", timeout=1800000)
4. 升级Playwright及Chromium版本
旧版本可能存在兼容性bug,更新到最新版本:
pip install --upgrade playwright playwright install chromium
内容的提问来源于stack exchange,提问作者Jerome Culcul
相关产品推荐
相关产品推荐

