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

时间:2020-06-10 20:09:46

标签: python selenium selenium-webdriver file-upload sendkeys

当我使用此代码时,使用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:消息:无效 参数:找不到文件:上传

即使我也通过这种方式进行了检查,然后还显示了错误。

  • C:/Users/Archi/Downloads/resume testing/Resume testing
  • C:\Users\Archi\Downloads\resume testing/Resume testing
  • C:\\Users\Archi\Downloads\resume testing/Resume testing

2 个答案:

答案 0 :(得分:0)

您使用什么语言?

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

string upload = @“ C:\ Users \ Archi \ Downloads \ Resume testing \ Resume testing”;

答案 1 :(得分:0)

你足够亲密。

您不想通过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)

参考

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