简体   繁体   English

Selenium WebDriver:上传多个文件

[英]Selenium WebDriver: Upload multiple files

My test need to upload test files in different browsers (I use WebDriver + Java ). 我的测试需要在不同的浏览器中上传测试文件(我使用WebDriver + Java )。 For a single file upload, everything works fine. 对于单个文件上传,一切正常。 I just send the path 我只是发送路径

"C:\\testdata\\testfile.txt"
But, syntax changes for multiple upload and different browsers.

(
IE: 
"\"" + "C:\\Selenium\\TestData\\Flexy - BigFile1.txt"+"\"" +"\""+"C:\\Selenium\\TestData\\Flexy - BigFile2.txt" + "\""

CHROME: 
"C:\\Selenium\\TestData\\Flexy - BigFile1.txt"+"\n"+"C:\\Selenium\\TestData\\Flexy - BigFile2.txt".

Firefox: I'm not able to find a correct syntax. Firefox:我无法找到正确的语法。

Any idea? 任何想法?

Is there a common syntax for all browsers? 所有浏览器都有通用语法吗?

As far as I know, selenium still does not support multiple file upload (see issue on google code ). 据我所知,selenium 仍然不支持多文件上传(请参阅谷歌代码上的问题 )。

There is at least one workaround: apparently create a form that contains as many input fields as you need (see another stackoverflow question ). 至少有一种解决方法:显然创建一个包含所需数量的输入字段的表单(请参阅另一个stackoverflow问题 )。 Not the best solution, as it (probably) requires altering your code for selenium to work. 不是最好的解决方案,因为它(可能)需要改变你的代码才能使selen工作。

However, as you have found out (thanks for this!), it does seem possible to trigger multiple file uploads in chrome and (although I did not test it) IE as well. 但是,正如你已经发现的那样(感谢你!),它似乎可以触发chrome中的多个文件上传和(虽然我没有测试它)IE也是如此。

I just confirmed that the chrome "\\n" trick works both locally and on Browserstack (I used the default images they provide), which, considering the state of things, is good enough for me. 我刚刚确认chrome“\\ n”技巧在本地和Browserstack都有效(我使用了他们提供的默认图像),考虑到事物的状态,这对我来说已经足够了。

I hope this helps. 我希望这有帮助。

The solution for me (selenium in python) was to just repeat send_keys for each image path before uploading. 我的解决方案(python中的selenium)只是在上传之前为每个图像路径重复send_keys。

Example for two files: 两个文件的示例:

driver.find_element_by_name("filename").send_keys(file_path_1)
driver.find_element_by_name("filename").send_keys(file_path_2)
driver.find_elements_by_xpath("//*[contains(text(), 'Upload')]")[0].send_keys(Keys.RETURN)

I also get chance to upload multiple files via Selenium. 我也有机会通过Selenium上传多个文件。

Finally get the solution using AutoIT. 最后使用AutoIT获得解决方案。

You can pass file path at run time. 您可以在运行时传递文件路径。

ControlFocus(“File Upload”,””,”Edit1″)
ControlSetText(“File Upload”,””,”Edit1″,$CmdLine[1])
ControlClick(“File Upload”,””,”Button1″)


Runtime.getRuntime().exec("C:\\Users\\Mukesh_50\\Desktop\\My blog\\AutoIT\\fileUpload3.exe"+" "+"C:\\Users\\Mukesh_50\\Downloads\\VerifyTitle.java");

If finding any issue then check complete article with video. 如果发现任何问题,请查看包含视频的完整文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM