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

问题描述 投票:0回答:1

当我使用这段代码时,在Selenium中使用Python上传文件时,它给出了错误,有人能帮助我吗?

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
python selenium selenium-webdriver file-upload sendkeys
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)

参考文献

你可以在:


0
投票

你用的是什么语言?

对于c#来说,如果路径是有效的,就使用@符号,然后使用 \\

string upload= @"C:Users/Archi/Downloads/resume testing/Resume testing"。

© www.soinside.com 2019 - 2024. All rights reserved.