Playwright(Python)文件上传报ENOENT错误:动态路径不存在求助
问题:Playwright(Python)动态生成文件路径上传失败
使用Playwright(Python)实现文件上传自动化时,除文件上传步骤外其余功能正常。脚本通过读取CSV数据生成唯一文件路径批量上传,但每次执行报错:
FileChooser.set_files: ENOENT: no such file or directory, access '/Users/dani/Library/CloudStorage/OneDrive-Personal/MCS-150 (Reactivate)/123456/Screenshot-2024-09-15-at-2.16.42 AM.png
报错代码
from playwright.sync_api import sync_playwright import time import csv file_path = '/Users/dani/Library/CloudStorage/OneDrive-Personal/Jotform Downloads/Jotform_Reactivate_Submissions.csv' with open(file_path, 'r') as file: csv_reader = csv.reader(file) next(csv_reader) for line in csv_reader: with sync_playwright() as p: browser = p.webkit.launch(headless=False, args=["--start-maximized"]) page = browser.new_page(no_viewport=True) page.goto("https://ask.fmcsa.dot.gov/app/ticket") page.set_default_timeout(60000) page.wait_for_load_state('networkidle') page.locator('xpath=//*[@id="rn_TextInput_5_Contact.Emails.PRIMARY.Address"]').fill('building.wealth@icloud.com') page.locator('xpath=//*[@id="rn_TextInput_7_Contact.Name.First"]').fill(line[0]) page.locator('xpath=//*[@id="rn_TextInput_9_Contact.Name.Last"]').fill(line[1]) page.locator('xpath=//*[@id="rn_TextInput_11_Incident.CustomFields.c.dot_number"]').fill(line[2]) page.locator('xpath=//*[@id="rn_TextInput_13_Incident.CustomFields.c.mc_number"]').fill(line[3]) page.locator('xpath=//*[@id="rn_ProductCategoryInputSimpleFiltered_20_Incident.Product_lvl1"]').select_option('USDOT Number') page.locator('xpath=//*[@id="rn_ProductCategoryInputSimpleFiltered_20_Incident.Product_lvl2"]').select_option('Reactivate my USDOT Number') time.sleep(15) ID_path = ('/Users/dani/Library/CloudStorage/OneDrive-Personal/MCS-150 (Reactivate)/{}/{}'.format(line[2], line[4])) LLC_path = ('/Users/dani/Library/CloudStorage/OneDrive-Personal/MCS-150 (Reactivate)/{}/{}'.format(line[2], line[5])) print(ID_path) print(LLC_path) with page.expect_file_chooser() as fc_info: page.get_by_label("Attach Documents").click() file_chooser = fc_info.value file_chooser.is_multiple() file_chooser.set_files(ID_path) time.sleep(10) file_chooser.set_files(LLC_path) time.sleep(30) browser.close()
手动固定路径可正常运行的代码
from playwright.sync_api import sync_playwright import time import csv file_path = '/Users/dani/Library/CloudStorage/OneDrive-Personal/Jotform Downloads/Jotform_Reactivate_Submissions.csv' with open(file_path, 'r') as file: csv_reader = csv.reader(file) next(csv_reader) for line in csv_reader: with sync_playwright() as p: browser = p.webkit.launch(headless=False, args=["--start-maximized"]) page = browser.new_page(no_viewport=True) page.goto("https://ask.fmcsa.dot.gov/app/ticket") page.set_default_timeout(60000) page.wait_for_load_state('networkidle') page.locator('xpath=//*[@id="rn_TextInput_5_Contact.Emails.PRIMARY.Address"]').fill('building.wealth@icloud.com') page.locator('xpath=//*[@id="rn_TextInput_7_Contact.Name.First"]').fill(line[0]) page.locator('xpath=//*[@id="rn_TextInput_9_Contact.Name.Last"]').fill(line[1]) page.locator('xpath=//*[@id="rn_TextInput_11_Incident.CustomFields.c.dot_number"]').fill(line[2]) page.locator('xpath=//*[@id="rn_TextInput_13_Incident.CustomFields.c.mc_number"]').fill(line[3]) page.locator('xpath=//*[@id="rn_ProductCategoryInputSimpleFiltered_20_Incident.Product_lvl1"]').select_option('USDOT Number') page.locator('xpath=//*[@id="rn_ProductCategoryInputSimpleFiltered_20_Incident.Product_lvl2"]').select_option('Reactivate my USDOT Number') time.sleep(15) ID_path = ('/Users/dani/Library/CloudStorage/OneDrive-Personal/MCS-150 (Reactivate)/{}/{}'.format(line[2], line[4])) LLC_path = ('/Users/dani/Library/CloudStorage/OneDrive-Personal/MCS-150 (Reactivate)/{}/{}'.format(line[2], line[5])) print(ID_path) print(LLC_path) with page.expect_file_chooser() as fc_info: page.get_by_label("Attach Documents").click() file_chooser = fc_info.value file_chooser.is_multiple() file_chooser.set_files('/Users/dani/Library/CloudStorage/OneDrive-Personal/MCS-150 (Reactivate)/123456/Screenshot-2024-09-15-at-2.16.42 AM.png') time.sleep(10) file_chooser.set_files('/Users/dani/Library/CloudStorage/OneDrive-Personal/MCS-150 (Reactivate)/123456/IMAGE-2024-09-1-at-2.16.55 AM.png') time.sleep(30) browser.close()
解决方案
- 检查CSV读取的文件名是否含不可见字符:CSV中的文件名可能带有全角空格、软空格或其他非打印字符,和手动输入的半角空格不一致。可以通过
print(repr(line[4]))输出文件名的原始字符表示,对比手动输入的文件名,确认是否有差异。 - 验证动态路径的有效性:在调用
set_files前,用os.path.exists检查文件是否真实存在,避免路径错误:import os if not os.path.exists(ID_path): print(f"文件不存在:{ID_path}") continue # 或抛出异常终止当前循环 - 优化文件选择器调用方式:如果支持多选,一次性传入路径列表,不要分两次调用
set_files,避免状态异常:file_chooser.set_files([ID_path, LLC_path]) - 清理文件名中的异常空格:对CSV读取的文件名做清洗,比如去除首尾空格、替换全角空格为半角:
ID_path = f'/Users/dani/Library/CloudStorage/OneDrive-Personal/MCS-150 (Reactivate)/{line[2]}/{line[4].strip().replace(" ", " ")}' - 确认OneDrive同步状态:OneDrive路径下的文件可能因未同步完成导致本地不存在,确保文件已同步到本地后再执行上传。
内容的提问来源于stack exchange,提问作者user28538867
相关产品推荐
相关产品推荐

