使用C#selenium上传文件而不使用SendKeys

时间:2018-06-04 12:13:26

标签: c# selenium upload

我想使用Selenium Webdriver和C#上传文件,但SendKeys在我的情况下不起作用,因为HTML没有输入类型=“文件”

我可以点击“按钮”并正确打开资源管理器,但之后我无能为力。 以下是要点击的HTML区域的一部分:

<div class="row drop-box cotacao-estudo" ngf-drop="vm.UploadFiles($files, vm.TipoDocumentoEnum.ESTUDO)" ngf-select="vm.UploadFiles($files, vm.TipoDocumentoEnum.ESTUDO)" ngf-change="vm.ValidateFiles($files, $file, $newFiles, $duplicateFiles, $invalidFiles, $event, vm.TipoDocumentoEnum.ESTUDO)" ngf-max-size="30MB" ngf-drag-over-class="'dragover'" ngf-multiple="true" ngf-allow-dir="true" accept="image/gif,image/jpeg,image/jpg,image/tiff,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,image/png,application/pdf,text/plain,application/vnd.ms-excel,application/excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/msword" ngf-pattern="'image/gif,image/jpeg,image/jpg,image/tiff,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,image/png,application/pdf,text/plain,application/vnd.ms-excel,application/excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/msword'">
    <i class="fa fa-folder fa-3x pull-left" style="color:#5bc0de" aria-hidden="true"></i>
    <label class="pull-left">Estudo *</label>
    <span class="pull-left">Clique para selecionar ou arraste os arquivos aqui</span>
    <div class="progress pull-left ng-hide" ng-show="vm.progressEstudo > 0">
        <div style="width:%;" ng-bind="vm.progressEstudo + '%'" class="ng-binding">%</div>
    </div>
</div>

以下是点击代码:

driver.FindElement(By.XPath("//fieldset[@class='ng-scope']/div[1]/i")).Click();

OR

driver.FindElement(By.ClassName("cotacao-estudo")).Click();

我已经尝试过这段代码而没有成功:

//string filePath = @"C:\Users\cccarvalho\Documents\AUTOMACOES\Estudo.pdf";

String caminho = "//fieldset[@class='ng-scope']/div[1]/i";

document.evaluate('caminho', document, null, 
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

String script = "driver.getElementByXpath('caminho').value='" + 
"C:\\\\file.txt" + "';";


((IJavaScriptExecutor)driver).ExecuteScript(script);

1 个答案:

答案 0 :(得分:-1)

打开资源管理器后,您可以发送文件的路径,如下所示。

<div id="test">Foo</div>