java - Selenium Webdriver - 由于“添加”按钮不可见,上传功能无法正常工作

标签 java user-interface file-upload selenium

摘要: 在 Selenium Webdriver 中 - 由于“添加”按钮不可见,上传功能无法正常工作。

描述:

功能类似于,打开一个弹出窗口用于上传文件,最初“添加”按钮被禁用。用户需要单击浏览按钮,选择文件,系统将验证文件格式和大小(4MB)。如果有效,则浏览文本框将以绿色突出显示。仅经过验证后,“添加”按钮才会启用。单击“添加”后,弹出窗口关闭,添加的 Material 显示在主页中。

Selenium 代码和错误:

但是下面的代码会抛出错误 - org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互。

driver.findElementByXPath("//*[@id='fuVideo_ctl02']").sendKeys("D:\\Featured         Materials");
Thread.sleep(1000);            
driver.findElementByClassName("add_uploadbtn").click(); //Clicking on Add button
String Addtext = driver.findElementByClassName("add_uploadbtn").getText();

System.out.println("添加文本"+Addtext);

以上打印报表结果: 添加文字

注意:在脚本运行时,文档会上传,文件验证会开始但不会结束。它会显示一段时间,脚本会以上述错误结束。

HTML: 上传功能代码直至添加按钮

<td colspan="2">
<span id="fuVideo" class="upload_file_txt mleft10" style="background: white"    onclick="javascript:fnCheckFileIsUpload(this);">
<input id="fuVideo_ctl00" class="upload_file_txt" type="hidden" name="fuVideo$ctl00" style="width: 550px;" value="Copy of CAS QEA-PES Score Card Feb_2014_Capex  Opex_STORM_Updated.xlsx">
<div id="fuVideo_ctl01" name="fuVideo_ctl01">
<input id="fuVideo_ctl02" class="upload_file_txt" type="file" style="width: 550px; background-color: Lime; color: black;" name="fuVideo$ctl02">
</div>
</span>
<br disabled="disabled">
<span style="margin-left: 25px;">Description</span>
<input id="txtDescriptionFU" class="add_edit_content_input" type="text" style="margin-left: 20px; width: 460px;" maxlength="500" name="txtDescriptionFU">
<input id="btnVideo" class="add_uploadbtn" type="button" onclick="javascript:fnFileUploadValidation();" value="Add">

请。帮助解决这个问题。

提前致谢......

最佳答案

1 秒似乎不足以让“添加”按钮变得可见。使用:

代替Thread.sleep(1000)
WebDriverWait wait = new WebDriverWait(driver, MAX_WAITTIME_IN_SECONDS);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("add_uploadbtn").click();

这假设 driver.findElementByXPath("//*[@id='fuVideo_ctl02']").sendKeys("D:... 工作正常。

关于java - Selenium Webdriver - 由于“添加”按钮不可见,上传功能无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22860429/

相关文章:

java - Android - 保存登录详细信息

python - 用于 python 源的 Windows GUI 应用程序?

ios - 从 iOS 应用程序上传文件到服务器

php - 两次使用不同的名称上传图像 php mysql

java - response.jsonPath() 元素周围有方括号,如何检索字符串值?放心

java - 无法访问 org.springframework.context.ConfigurableApplicationContext 类文件

java - 如何使用 Maven 管理项目依赖项?

java - 如何根据组合框选择更改 UI

java - 在 java swing 中定位按钮

php - mysql: 不能将 max_allowed_pa​​cket 设置为任何大于 16MB 的值