上传图像时文件大小出错

时间:2015-04-09 22:49:53

标签: selenium selenium-webdriver

我正在尝试使用以下脚本上传文件。脚本通过浏览按钮上传文件,但是当我点击上传按钮时,它会出现错误//文件大小错误。

仅供参考 - 手动上传同一文件是成功的

我的剧本:

driver.findElement(By.xpath(".//*[@id='fileUploadInput']/div/div[2]/input[2]")).sendKeys(file_upload);
driver.findElement(By.id("btnCreativeHostingFileUpload")).click();

HTML:

<tr id="fileOas" style="display: table-row;">
<td align="left" style="height:25px;">File Name</td>                                                                            
<td nowrap="" align="left" style="height:40px;">
<input type="hidden" style="width:250px" name="uploadFileName" id="videoUploadFileName" value="tag.png">
<div onclick="setActiveUploadForUploadAll();" style="height:40px;">
<!-- <iframe id="sUploadAll" name="sUploadAll" onmouseover="setActiveUpload('sUploadAll');" -->
<iframe width="470" height="40" frameborder="no" allowtransparency="true" scrolling="no" src="/richmedia/oas/FileUpload.do?nextAction=/oas/adwizard.banner.update.do&amp;currentPage=/oas/adwizard.creative.index.do&amp;fileCategory=image&amp;fieldName=videoUploadFileName&amp;previousFile=" onmouseover="prepareActiveUpload('sUploadAll');" name="sUploadAll" id="sUploadAll">
</iframe>
<input type="hidden" name="videoUploadFileNameFullUrl" id="videoUploadFileNameFullUrl" value="tag.png">
 <input type="hidden" name="videoUploadFileDownloadUrl" id="videoUploadFileDownloadUrl">
<input type="hidden" value="" id="currentSelectFileId">
<input type="hidden" value="" id="currentSelectFileType">
 <input type="hidden" name="uploadFileNameFullUrl" id="uploadFileNameFullUrl" value="tag.png">
<input type="hidden" name="dimensionWidth" id="dimensionWidth">
 <input type="hidden" name="dimensionHeight" id="dimensionHeight">
</div>
</td>                                                                           
</tr>


<div>
    <div class="input_div">
        <input type="text" readonly="readonly" class="file_input_textbox" id="fileName">
    </div>
    <div class="file_input_div">
        <input type="button" value="Browse" class="file_input_button">
        <input type="file" style="width:100px;" onchange="javascript: document.getElementById('fileName').value = this.value" value="" size="25" name="theFile">
    </div>
    <div class="upload_btn_div">
        <button onclick="showProgressAndUpload(this.form);" name="btnCreativeHostingFileUpload" id="btnCreativeHostingFileUpload" type="button" style="margin-left: 10px" class="btn btn_gray"><div><p>Upload</p></div></button>
    </div>
</div>

1 个答案:

答案 0 :(得分:2)

看起来你使用了错误的选择器

试试这个cssSelector

[type='file']

或者,该输入标签也有名称。因此,您可以将theFile用作By.name

所以,那就是我在你提供的html中看到的唯一文件输入标签

如果上述方法不起作用,您也可以使用JavaScript执行程序执行此操作

String filePath = "the filepath with extension";
((JavascriptExecutor)driver).executeScript("document.getElementById('fileName').setAttribute('value', '"+filePath+"');");
driver.findElement(By.id("btnCreativeHostingFileUpload")).click();