繁体   English   中英

无法在输入元素上使用sendKeys上传文件

[英]Unable to upload file using sendKeys on input element

为了避免上传文件的窗口对话框,我正在尝试以下操作:

driver.findElement(By.xpath("//span[@class='ab-attachment-item']/input")).sendKeys(filePath);

以下是HTML代码段:

<div class="ab-attachments">
  <span class="ab-attachment-item" ng-hide="isReadOnly()" style="background-color: transparent;">
    <input class="ab-attachment-input ng-isolate-scope firefinder-match" type="file" rx-file-upload="file" accept=".pdf,image/*" style="background-color: transparent;">

但这会导致错误:

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

我想确认此错误是否是由于span标签中的ng-hide="isReadOnly()"

以及如何使用Selenium WebDriver本身(使用JavaScriptExecutor等)解决此问题?

为了处理该对话框,可以使用其他工具,例如Sikuli,AutoIt; 但我想避免那笔开销。

您应该可以执行以下操作:

driver.findElement(By.className("ab-attachment-input ng-isolate-scope firefinder-match")).sendKeys(filePath);

如果没有尝试添加隐式的等待,然后再尝试和sendKeys

Int timeoutInSeconds = 10;
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("ab-attachment-input ng-isolate-scope firefinder-match"));

//我将使用隐式等待并直接调用输入元素

new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[rx-file-upload='file'][accept=".pdf,image/*"]"))).sendKeys("filePathWithExtension");

暂无
暂无

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

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