Selenium WebDriver上传文件C# [英] Selenium WebDriver upload file C#

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

问题描述

我使用Selenium驱动程序将文件上传到Web系统时遇到问题.因此,在我的页面中,我没有任何输入来设置文件路径,而且我在网络中建立的大多数解决方案都不适合我.我必须单击上传文件"按钮,然后在窗口对话框中设置文件的路径.我找到了用于winforms SendKeys类的方法:

I have a problem with upload file to Web system using selenium driver. So in my page I haven't any input to set file path and most of solutions which I founded in network not work for me. I have to click on upload file button and then set path to the file in window dialog. I found method which used from winforms SendKeys class:

SendKeys.SendWait(filePath);
SendKeys.SendWait(@"{Enter}");

它起作用,但是仅在测试完全控制了计算机的情况下才起作用(如果不是,则-路径已发送到某个地方而不是对话框).这就是问题所在,因为我无法完全控制运行测试的计算机.您知道不使用SendKeys上传文件的方法吗?

it works but only when the tests has full control of the machine (if not - the path have been sending somewhere not to dialog). And this is the problem because I'm not be able to get full control to machine where the test are running. Do you know a way how to upload file do not use SendKeys?

推荐答案

可能我已经解决了这个问题.以下是Java中的示例,但该解决方案的要点是创建AutoIt脚本,然后从C#代码运行exe文件.

Probably I solved the problem. Below is example in Java how to do it but main point of that solution is create AutoIt script and then run exe file from C# code.

解决方案

应更改示例脚本,因为它仅适用于IE:

Sample script should be changed because it works only on IE:

WinWaitActive("Choose file")

该行排除其他浏览器.例如,在Chrome上,该窗口名为打开".创建并编译脚本后足以调用:

That line exclude other browsers. On Chrome for example the window named as "Open". After create and compile script enough to call:

Process.Start(filePath);

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

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