Java 无法使用输入元素上的sendKeys上载文件

Java 无法使用输入元素上的sendKeys上载文件,java,javascript,angularjs,selenium,selenium-webdriver,Java,Javascript,Angularjs,Selenium,Selenium Webdriver,为了避免上传文件的窗口对话框,我尝试以下操作: 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-colo

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

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;">
我想确认此错误是否是由于span标记中的ng hide=isReadOnly造成的

如何使用SeleniumWebDriver本身和JavaScriptExecutor之类的工具来解决这个问题


对于处理此对话框,可以使用其他工具,如Sikuli、AutoIt;但我想避免这种开销。

您应该能够做到这一点:

driver.findElement(By.className("ab-attachment-input ng-isolate-scope firefinder-match")).sendKeys(filePath);
如果没有,请尝试添加隐式等待,然后再尝试发送密钥


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

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

Saifur,我也试过了,但它给出了错误:org.openqa.selenium.TimeoutException:在等待…的可见性10秒后超时。顺便问一下,这是一个可见元素吗?如果是这样,使用findelEmens查看它返回了多少元素。我怀疑有多个元素,选择器也找不到正确的元素。这很有道理。Saifur,我试过了,它返回1意味着这是唯一的元素。可能是由于span标记中的ng hide=isReadOnly而不可见,正如我在question.TidusJar中提到的,我也尝试过这个,但它给出了错误:org.openqa.selenium.TimeoutException:在等待…的可见性10秒后超时。发生这种情况的原因是WebDriver Wait找不到元素。这说明您没有正确定位元素。如果我没有正确定位元素,它不会给出错误:org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互,而会给出错误:org.openqa.selenium.NoSuchElementException:无法定位元素:仅当找到元素时才会检查元素的可见性。因为您正在执行此操作与元素交互之前的隐式等待,也就是它超时的地方。它在超时时间段10秒内找不到元素。
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");