简体   繁体   English

使用不带SendKeys的C#硒上传文件

[英]Upload file using C# selenium without SendKeys

I want to upload a file using Selenium Webdriver with C#, but the SendKeys doesn´t work in my case, because the HTML does not have the input type="file" 我想使用带有C#的Selenium Webdriver上传文件,但是在我的情况下,SendKeys不起作用,因为HTML没有输入type =“ file”

I can click on the "button" and the explorer open correctly, but after that I couldn´t do anything. 我可以单击“按钮”,浏览器将正确打开,但是此后我什么也不能做。 Here is the part of HTML area to click: 这是要单击的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>

Here is the code of the click: 这是点击的代码:

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

OR 要么

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

I´ve already tried this code without success: 我已经尝试过此代码,但未成功:

//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);

After explorer is opened you can send the path of the file as below. 打开资源管理器后,您可以按以下方式发送文件的路径。

SendKeys.SendWait(@"Path of file");

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

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

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