通过Selenium在AngularJS中自动上传文件

Automate file upload in AngularJS via Selenium

本文关键字:自动上传 文件 AngularJS Selenium 通过      更新时间:2023-09-26

我正在使用Powershell驱动.NET Selenium和FirefoxDriver来自动化一些东西。其中一部分是文件上传,网站恰好(至少部分)使用AngularJS编写。

现在,我已经了解了如何使用普通输入元素自动上传文件。只需通过SendKeys发送文件路径。

但在这种情况下我想不通。带有可选手动文件选择器的文件放置区域的HTML如下所示:

<div class="overflowHidden video-drop-zone file-drop-zone zone appversionicon rounded"
ng-file-drop="onFileSelect($files);" ng-file-drop-available="dropSupported=true">               
    <div class="simpleDropZoneFileSelect">
        <span class="selectFileText">
            <span class="ng-binding ng-hide" ng-show="dropLabel !== undefined &amp;&amp; dropLabel !== ''"><br></span>
            <a class="ng-binding" href="" ng-show="true">Select file</a>
            <input class="" ng-show="true" ng-file-select="onFileSelect($files)" type="file">
        </span>
    </div>
</div>

我希望我没有把这件事简化太多。整个AngularJS设置肯定不止于此。但也许你给我一些建议就足够了,告诉我下一步该往哪里看,或者如何解决这个问题。如果没有,请告诉我,我会添加更多信息。

我发现Protractor似乎是进行AngularJS测试的方法,但它会大大改变我的设置(使用NodeJS服务器等),我现在只需要上传这个文件。

谢谢!

Sandro

不确定整个设置是什么样子的。但文件上传在硒中要容易得多。

Driver.FindElement(By.CssSelector("input[type='files']")).SendKeys("FilePath") 

应该这样做

<button class="btn btn-primary ng-scope" ng-click="vm.importAccountsClicked()" translate="import-accounts">Import Accounts</button>
<input class="hide" type="file" id="fileItem" accept=".csv" onchange="angular.element(this).scope().import()">

我在使用Webdriver和Java时遇到了类似的问题。查看一个带有"导入帐户"按钮的网页(上面的html片段),我无法让Selenium+Java向其发送密钥。我所做的错误是没有使用type=file属性来识别元素。相反,我使用的是按钮的文本:

@FindBy (xpath="(//button[.='Import Accounts'])")
private WebElement importbutton;

所以在我将importbutton变量声明更改为之后

@FindBy (id = "fileItem")
private WebElement importbutton;

使用sendKeys()方法解决了该问题。

importbutton.sendKeys(filepath);

希望这能有所帮助。