selenium.common.exceptions.InvalidArgumentException:消息:参数无效:使用 Selenium 上传文件时找不到文件错误 Python

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found error while uploading file using Selenium Python

当我使用此代码时,使用 Python 在 Selenium 中上传文件时出错,有人可以帮我解决这个问题吗?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver=webdriver.Chrome(executable_path="C:\Users\Archi\PycharmProject\chrome driver\chromedriver")
driver.get("https://www.freshersworld.com/user/register")

driver.implicitly_wait(10)

upload="C://Users/Archi/Downloads/resume testing/Resume testing"
driver.find_element_by_id("file-upload").send_keys("upload")

错误:

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found : upload

连我也是用这种方式查的,然后也是报错

您使用的是什么语言?

对于c#,如果路径有效,使用@符号并使用\

字符串上传=@"C:\Users\Archi\Downloads\resume testing\Resume testing";

你已经足够接近了。

您不想通过 send_keys() 传递字符序列 上传,而是想传递文件 C://Users/Archi/Downloads/resume testing/Resume testing

因此您需要进行两 (2) 处更改,如下所示:

  • 使用不同的路径分隔符,即 /\
  • 添加文件扩展名,例如.doc

因此,您的有效代码块将是:

upload="C:\Users\Archi\Downloads\resume testing\Resume testing.doc"
driver.find_element_by_id("file-upload").send_keys(upload)

参考

您可以在以下位置找到相关讨论: