Merge pull request #403 from xpzouying/fix/upload-images-one-by-one

fix: 多图上传改为逐张上传,适配页面 DOM 变化
This commit is contained in:
tan jun
2026-02-06 21:08:01 +08:00
committed by GitHub

View File

@@ -199,8 +199,6 @@ func isElementBlocked(elem *rod.Element) (bool, error) {
} }
func uploadImages(page *rod.Page, imagesPaths []string) error { func uploadImages(page *rod.Page, imagesPaths []string) error {
pp := page.Timeout(30 * time.Second)
// 验证文件路径有效性 // 验证文件路径有效性
validPaths := make([]string, 0, len(imagesPaths)) validPaths := make([]string, 0, len(imagesPaths))
for _, path := range imagesPaths { for _, path := range imagesPaths {
@@ -209,18 +207,31 @@ func uploadImages(page *rod.Page, imagesPaths []string) error {
continue continue
} }
validPaths = append(validPaths, path) validPaths = append(validPaths, path)
logrus.Infof("获取有效图片:%s", path) logrus.Infof("获取有效图片:%s", path)
} }
// 等待上传输入框出现 // 逐张上传:第一张用 .upload-input后续页面会移除该 class改用 input[type="file"]
uploadInput := pp.MustElement(".upload-input") for i, path := range validPaths {
selector := `input[type="file"]`
if i == 0 {
selector = ".upload-input"
}
// 上传多个文件 pp := page.Timeout(60 * time.Second)
uploadInput.MustSetFiles(validPaths...) uploadInput, err := pp.Element(selector)
if err != nil {
return errors.Wrapf(err, "查找上传输入框失败(第%d张)", i+1)
}
if err := uploadInput.SetFiles([]string{path}); err != nil {
return errors.Wrapf(err, "上传第%d张图片失败", i+1)
}
slog.Info("图片已提交上传", "index", i+1, "path", path)
time.Sleep(1 * time.Second)
}
// 等待并验证上传完成 // 等待并验证上传完成
return waitForUploadComplete(pp, len(validPaths)) return waitForUploadComplete(page, len(validPaths))
} }
// waitForUploadComplete 等待并验证上传完成 // waitForUploadComplete 等待并验证上传完成