【问题标题】:selenium.common.exceptions.WebDriverException: Message: File not found: /path/to/fileselenium.common.exceptions.WebDriverException:消息:找不到文件:/path/to/file
【发布时间】:2017-08-17 18:53:12
【问题描述】:

html 部分如下所示:

...
<form action="/upload/select-single/" method="post" enctype="multipart/form-data">
  <input type="hidden" name="csrfmiddlewaretoken" value="U3Uh3EAYWXaWEiNaWC3zcCQutQe8hGM53u4BTx810o08t1GwOfIJI2ayHCFQFySw">
  <p>
    <label for="id_file_field">File field:</label>
    <input id="id_file_field" name="file_field" type="file" multiple="" required="">
  </p>
  <input id="submit" type="submit" value="submit">
</form>
...

这样的硒测试:

...
btn_choose_file = self.browser.find_element_by_id("id_file_field")
# write file path to field
# to check that I really selected the field, I tried btn_choose_file.click() -> and it works
btn_choose_file.send_keys('/path/to/file')  # -> THIS is the line which seems to cause the problem
# click submit
self.browser.find_element_by_id("submit").click()
...

不幸的是,在执行测试时会引发此错误

selenium.common.exceptions.WebDriverException:消息:找不到文件:/path/to/file

这是由于文件路径通过btn_choose_file.send_keys('/path/to/file')发送到输入元素的部分

注意:真正的“/path/to/file”如下

  • /Users/udos/development/trails/processfile/gpx_data/test_data/suite 001 - val poschiavo/Bernina - Le Prese.gpx(文件存在,我从 IDE 复制了路径)

非常奇怪的行为。这个测试之前运行过,不知何故我设法打破了它......

任何建议可能是什么原因?

更新
将 Firefox 从 55.0.2 版降级到 54.0.1 版“修复”了该问题。
请参阅 -> https://stackoverflow.com/a/45753912/420953

【问题讨论】:

  • 只是一个建议。您可以尝试从路径中删除空格。
  • 感谢!我用'/Users/udos/development/trails/processfile/gpx_data/test_data/other/data.gpx'试过了,同样的错误:|
  • 你能告诉我们确切的 HTML 和你的确切代码行以及确切的错误吗?
  • 当然。刚刚做了。对于那个很抱歉。我还在硒测试中添加了一些 cmets
  • 将文件放在“/tmp”中,不留空格,然后用更小的名称重试?

标签: python-3.x selenium


【解决方案1】:

解决此问题的一种方法是将文件名中出现的所有“/”替换为“\”(python 中为“\\”)。

这至少在 Windows 上有效。

【讨论】:

    【解决方案2】:

    在这个错误之后,我安装并使用了 chromewebdriver。像 Firefox 驱动程序一样工作,我的所有测试都运行顺利。我建议你尝试一下,至少它对我来说就像一个魅力

    【讨论】:

      【解决方案3】:

      此错误是由于 Firefox 更新到版本 55.0.2(由于我配置了自动更新而在 2 天前自动发生)

      以下 geckodriver 问题将我引向了这个方向

      然后我按照文章中的描述将 Firefox 版本降级到 54.0.1

      这样,硒测试再次运行。

      【讨论】:

        猜你喜欢
        • 2018-02-14
        • 2019-10-06
        • 2019-06-15
        • 2018-07-11
        • 1970-01-01
        • 2021-06-16
        • 2019-05-28
        • 1970-01-01
        • 2016-04-19
        相关资源
        最近更新 更多