簡體   English   中英

使用不帶SendKeys的C#硒上傳文件

[英]Upload file using C# selenium without SendKeys

我想使用帶有C#的Selenium Webdriver上傳文件,但是在我的情況下,SendKeys不起作用,因為HTML沒有輸入type =“ file”

我可以單擊“按鈕”,瀏覽器將正確打開,但是此后我什么也不能做。 這是要單擊的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();

要么

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

打開資源管理器后,您可以按以下方式發送文件的路徑。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM