无法使用Selenium上传文件 [英] Failing to upload a file using Selenium

查看:132
本文介绍了无法使用Selenium上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用eclipse上的以下代码使用Selenium将文件上传到表单:

I'm trying to upload a file to a form using Selenium using this code on eclipse:

search = driver.find_element_by_xpath("//input[@type='file']")
search.send_keys("D:/test.txt")
search.send_keys(Keys.RETURN) 

此错误不断出现:

selenium.common.exceptions.WebDriverException:消息:文件不存在 找到:D:/test.txt

selenium.common.exceptions.WebDriverException: Message: File not found: D:/test.txt

文件到位,您认为问题出在哪里?

The file is in place, where do you think the problem is?

推荐答案

我想原因是在路径中使用的斜杠内-我认为它需要反斜杠.

I guess the reason is within the slash used in the path - I think it requires a backslash instead.

如果您尝试使用search.send_keys("D:\\test.txt")怎么办?不确定是否需要双反斜杠,因此您也可以尝试使用单反斜杠.

What if you try to use search.send_keys("D:\\test.txt")? Not sure if double backslash is required for that, so you can try with single one as well.

编辑

我只用输入[type = file]和提交"按钮以简单的形式尝试了自己的代码:

I tried my own code on simple form with just the input[type=file] and with Submit button:

search = browser.find_element_by_xpath("//input[@type='file']")    
search.send_keys("F:\\test.txt")                                   
submit = browser.find_element_by_css_selector("input[type=submit]")
submit.click()

以某种方式,它工作得很好,只需要转义反斜杠并使用Submit按钮而不是ENTER按钮即可.

And somehow, it worked just fine, just had to escape backslash and to use Submit button instead of using ENTER button.

因此,请确保文件实际位于发布路径内,并且这样的代码(至少在Windows上)可以正常工作.此外,您还应该确保拥有对该文件的权限.

So make sure your file is actually there, within the path you posted, and such code (at least on Windows) works just fine. Also, you should make sure you have permission to this file.

这篇关于无法使用Selenium上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆