【问题标题】:selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found error while uploading file using Selenium Pythonselenium.common.exceptions.InvalidArgumentException:消息:无效参数:使用 Selenium Python 上传文件时找不到文件错误
【发布时间】:2020-09-30 09:12:38
【问题描述】:

当我使用此代码时,它会在 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

【问题讨论】:

  • 看起来你有send_keys("upload"),而你应该有send_keys(upload)而不带引号。
  • 感谢您的纠正,但是当我删除引号时,我也会收到错误 selenium.common.exceptions.InvalidArgumentException:消息:无效参数:找不到文件:
  • 先生,我认为我使用了错误的斜杠 / ,\ 我应该使用哪个。我从各个方面检查.. 谢谢

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


【解决方案1】:

你已经够近了。

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

参考

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

【讨论】:

  • 非常感谢。现在它的工作。我添加了文件扩展名 .docx
【解决方案2】:

你使用什么语言?

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

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

【讨论】:

  • 与我正在使用的python
猜你喜欢
  • 2019-08-03
  • 2020-05-02
  • 1970-01-01
  • 2018-12-28
  • 1970-01-01
  • 2021-05-17
  • 2022-01-01
  • 1970-01-01
相关资源
最近更新 更多