【发布时间】: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