使用Selenium WebDriver(ChromeDriver 2.12)在VisualForce页面中使用文件上载苦苦挣扎

时间:2014-12-23 04:19:28

标签: java file selenium upload visualforce

在使用VisualForce File Upload对话框时需要一些帮助。它与Selenium IDE完美搭配,但是当我尝试使用WebDriver时,驱动程序甚至无法找到元素,更不用说使用它们了。

以下是我正在尝试测试的文件上传器的HTML代码:

<div class="form-group">
<label class="col-xs-12">
File
<span class="required">*</span>
</label>
<div class="col-xs-12">
<input id="files" type="file" name="files">
</div>
</div>

这是我的Selenium IDE代码,它在文件上传器中键入文件的路径(效果很好):

<td>type</td>
<td>id=files</td>
<td>C:\Users\filefolder\file.rtf</td>

这是我的Selenium WebDriver代码(不起作用):

try{
    driver.findElement(By.id("files")).sendKeys("C:\\Users\\filefolder\\file.rtf");
}
catch(Exception e){
    verificationErrors.append("Could not work with file input; "+e.toString());
}

当我运行它时,这是输出: java.lang.AssertionError:无法使用文件输入; org.openqa.selenium.NoSuchElementException:没有这样的元素

如果您需要更多详细信息,请与我们联系。 非常感谢你的帮助!!

1 个答案:

答案 0 :(得分:0)

尝试使用明确的等待,让我知道该网页是否包含多个type='file'的元素

By locator = By.cssSelector("[type='file'][id='files']")
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
driver.findElement(locator).sendKeys("C:\\Users\\filefolder\\file.rtf");